【问题标题】:How to access "user.config" of different application如何访问不同应用程序的“user.config”
【发布时间】:2017-09-15 17:31:05
【问题描述】:

应用程序的 user.config 文件可以在 C# 中通过以下方式访问 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath

现在,我需要获取不同应用程序的 user.config,由其 exe 的完整路径标识,以便将当前应用程序 user.config 替换为该应用程序。

有什么建议吗?

编辑:请注意我对 user.config (ConfigurationUserLevel.PerUserRoamingAndLocal) 感兴趣,而不是 app.config 或 Application.exe.config 等应用程序级别设置。后者可由 OpenExeConfiguration(string) 访问,但不是我想要的。

【问题讨论】:

标签: c# migration settings


【解决方案1】:

overload that accepts a string 呢?

var config = ConfigurationManager.OpenExeConfiguration(pathToAssebmly);

无论如何,以这种方式将您的程序集绑定到另一个程序集是个坏主意。您应该考虑将配置文件复制到所有程序集中并更改特定于给定程序的部分。

编辑:由于您对特定于用户的设置感兴趣,您可以使用取自 here 的以下内容:

string configFile =  string.Concat(appName, ".config");  
// Map the new configuration file.
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap 
{ 
    ExeConfigFilename = configFile 
};

var config = ConfigurationManager.OpenMappedExeConfiguration(
        configFileMap, 
        ConfigurationUserLevel.PerUserRoamingAndLocal);

【讨论】:

  • 谢谢,这完全导致了正确的解决方案。但是configFileMap的Property LocalUserConfigFileName是string.Empty,这导致了一个Execption
  • 那么我想你必须设置这个属性而不是ExeConfigFilename t 你的配置文件的位置。
  • 这正是问题所在,因为这条路径的计算方式我无法重现,请参阅link。我认为这可以在没有逆向工程的情况下自动完成
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 2021-05-03
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 2013-12-30
相关资源
最近更新 更多