简体   繁体   English

如何获取其他应用程序的app.config并进行修改

[英]How to obtain app.config of a different application and modify it

I have two windows application. 我有两个Windows应用程序。 eg ., FormA and FormB 例如,FormA和FormB

The app.config of FormA is as below FormA的app.config如下所示

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="company" value="DSRC"/>

    </appSettings>

<connectionStrings>

<add name="test" connectionString="Testing Connection String"/>

</connectionStrings>

</configuration>

Now I have another application named as Form B. 现在我有另一个名为Form B的应用程序。

I want to retrieve both appsettings and connectionstrings of Form A into Form B. 我想将表单A的appsettings和connectiontrings检索到表单B.

Further I should be able to modify both of these appsettings and connection strings and save it into the Form A. 此外,我应该能够修改这些appsettings和连接字符串,并将其保存到表单A.

I know how to retrieve the appsettings , and connection strings of the same application and modify. 我知道如何检索appsettings,以及相同应用程序的连接字符串并进行修改。

But how do I obtain of some other application and modify the same. 但是我如何获得其他一些应用程序并修改它们。

Kindly do let me know. 请告诉我。

Actually I have 4 windows services running under one setup., one webservice and one wcf service and one application. 实际上我在一个设置下运行了4个Windows服务,一个web服务和一个wcf服务以及一个应用程序。 All these have different app.configs, comprising of different appsettings and different connection strings. 所有这些都有不同的app.configs,包括不同的appsettings和不同的连接字符串。 I am supposed to create a windows application that will retrieve each of these settings and then save it accordingly. 我应该创建一个Windows应用程序,它将检索每个设置,然后相应地保存它。

I tried upto this level 我试过这个级别

ExeConfigurationFileMap filename= new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"D:\Home\FormA\FormA\bin\Debug\FormA.exe.config";


Configuration config =
   ConfigurationManager.OpenMappedExeConfiguration(filename,
   ConfigurationUserLevel.None);

But then just got struck, I just do not know how to proceed further (Sounds dumb right !) 但是刚刚被击中,我只是不知道如何继续前进(声音愚蠢吧!)

Can anyone help me proceed down the way. 任何人都可以帮助我继续前进。

Regards cmrhema 关心cmrhema

Basically, you need to open the configuration for that other executable something like this: 基本上,您需要打开其他可执行文件的配置,如下所示:

// full path to other EXE, including EXE extension - but *NOT* .config !!
string otherExePath = @"C:\........\OtherApp\bin\Debug\OtherApp.exe";
Configuration otherConfig = 
              ConfigurationManager.OpenExeConfiguration(otherExePath);

and then you can access all the settings on the new otherConfig configuration: 然后您可以访问新的otherConfig配置上的所有设置:

string otherSetting = otherConfig.AppSettings.Settings["TestSetting1"].Value;

and you can also save it back (provided you have the necessary permissions to that directory). 并且您还可以将其保存回来(前提是您拥有该目录的必要权限)。

otherConfig.Save():
            ExeConfigurationFileMap fileMap2 
                = new ExeConfigurationFileMap();
            fileMap2.ExeConfigFilename = @"OtherFile";

            Configuration config2 =
              ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            ConnectionStringSettings newSettings = 
                config.ConnectionStrings.ConnectionStrings["oldSConString"];

            ConnectionStringsSection csSection 
                = config2.ConnectionStrings;

            csSection.ConnectionStrings.Add(newSettings);                
            config2.Save(ConfigurationSaveMode.Modified);

VS2005 C# Programmatically change connection string contained in app.config VS2005 C#以编程方式更改app.config中包含的连接字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM