Open other program's configuration files

怎甘沉沦 提交于 2019-11-27 14:01:02

问题


I have a program A, it also have an app.config file where I have added some keys like server address, username and password for connecting to a server. It is a console application. Now I want to make a UI which I have done. In that UI I want to modify the content of app.config of program A. How do I do that?

Here is what I tried, I copied the UI (basically an .exe) to program A's directory, where the app.config also resides. Then in the UI, I use the ConfigurationManager class's OpenExeConfiguration method, and passing the program A's filename as an argument. But it throws and exception of type System.Configuration.ConfigurationErrorsException.

So I think that my approach is incorrect. How shall I do this?

EDIT: Oh I forgot to tell I'm using C#, .NET 3.5 and VS 2008 (if that helps :D)


回答1:


I'm not sure about the problem with your approach (try adding the stack trace to your post) but this is how I do it:

var configMap = 
    new ExeConfigurationFileMap
    {
        ExeConfigFilename = externalConfigurationFile
    };
System.Configuration.Configuration externalConfiguration =
    ConfigurationManager.OpenMappedExeConfiguration(
        configMap,
        ConfigurationUserLevel.None);

foreach (var setting in externalConfiguration.AppSettings.Settings)
{
    ...
}

externalConfiguration.Save(ConfigurationSaveMode.Full);


来源:https://stackoverflow.com/questions/938585/open-other-programs-configuration-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!