Working with Multiple Web Config C#
I have 2 projects, each one have a WebConfig file, and i want to edit the 2 webconfig files from one place, and i try to do this:
string configPath = "/WebSite Name";
Configuration con开发者_JAVA百科fUI = WebConfigurationManager.OpenWebConfiguration("~");
Configuration confProtocol = WebConfigurationManager.OpenWebConfiguration(configPath);
AppSettingsSection appSettingsUI = (AppSettingsSection)confUI.GetSection("appSettings");
AppSettingsSection appSettingsProtocol = (AppSettingsSection)confProtocol.GetSection("appSettings");
if (appSettingsUI != null & appSettingsProtocol != null)
{
appSettingsUI.Settings[key].Value = value;
appSettingsProtocol.Settings[key].Value = value;
confUI.Save();
confProtocol.Save();
}
Also i try
Configuration confProtocol = WebConfigurationManager.OpenWebConfiguration(configPath,"webSiteName");
The problem that confProtocol is set to empty.
How to set The WebConfig path of the Protocol Project from the UI Project?
Hm. I guess checking "~" path and "configPath" will be a good starting point.
If you want the root web.config just pass null to the OpenWebConfiguration.
Configuration confProtocol = Configuration.OpenWebConfiguration(null);
If they are in different host then you need to specify hosts.
Configuration confProtocol = WebConfigurationManager.OpenWebConfiguration(configPath,"Protocol Web Site");
//your code is good but need a little modification
string appName = Environment.GetCommandLineArgs()[2];
//this should return the virtual path of the current site ex: "e:\Code\Website" //remove the last website and add the second website
string configFile = string.Concat(appName, "web.config");
ExeConfigurationFileMap configFMap = new ExeConfigurationFileMap();
configFMap.ExeConfigFilename = configPath;
Configuration confProtocol = ConfigurationManager.OpenMappedExeConfiguration(configFMap, ConfigurationUserLevel.None);
string configPath = "/WebSite Name";
Configuration confUI = WebConfigurationManager.OpenWebConfiguration("~");
Configuration confProtocol = WebConfigurationManager.OpenWebConfiguration(configPath);
AppSettingsSection appSettingsUI = (AppSettingsSection)confUI.GetSection("appSettings");
AppSettingsSection appSettingsProtocol = (AppSettingsSection)confProtocol.GetSection("appSettings");
if (appSettingsUI != null & appSettingsProtocol != null)
{
appSettingsUI.Settings[key].Value = value;
appSettingsProtocol.Settings[key].Value = value;
confUI.Save();
confProtocol.Save();
}
精彩评论