打开其他程序的配置文件 [英] Open other program's configuration files

查看:23
本文介绍了打开其他程序的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序 A,它还有一个 app.config 文件,我在其中添加了一些密钥,如服务器地址、用户名和密码以连接到服务器.它是一个控制台应用程序.现在我想制作一个我已经完成的用户界面.在那个 UI 中,我想修改程序 A 的 app.config 的内容.我怎么做?

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?

这是我的尝试,我将 UI(基本上是 .exe)复制到程序 A 的 目录,app.config 也驻留在该目录中.然后在 UI 中,我使用 ConfigurationManager 类的 OpenExeConfiguration 方法,并将程序 A 的 文件名作为参数传递.但它会抛出 System.Configuration.ConfigurationErrorsException 类型的异常.

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?

哦,我忘了告诉我我正在使用 C#、.NET 3.5 和 VS 2008(如果有帮助:D)

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

推荐答案

我不确定您的方法是否存在问题(尝试将堆栈跟踪添加到您的帖子中),但我是这样做的:

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);

这篇关于打开其他程序的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆