// Show how to use OpenExeConfiguration(string). static void DisplayAppSettingsRawXml() { // Get the application path. string exePath = System.IO.Path.Combine( Environment.CurrentDirectory, "ConfigurationManager.exe"); // Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(exePath); // Get the AppSetins section. AppSettingsSection appSettingSection = config.AppSettings; // Display raw xml. Console.WriteLine(appSettingSection.SectionInformation.GetRawXml()); }
使用System.Xml命名空间中所提供的方法,选择性更为大
2楼那个还是读取默认的 .config文件。
我需要任意xml啊。 不是根据程序获取的xml。
那就把Environment.CurrentDirectory改成你的路径呀!
就是。。不过还有点问题。参数key 在哪设置?
设置完路径后就可以了,不过要用你设置的那个变量config获取。 我记得好像是ConfigurationSettings.AppSettings["A"]<add key="A" value="config with A"/> 你试一下。
// Show how to use OpenExeConfiguration(string).
static void DisplayAppSettingsRawXml()
{
// Get the application path.
string exePath = System.IO.Path.Combine(
Environment.CurrentDirectory, "ConfigurationManager.exe"); // Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(exePath); // Get the AppSetins section.
AppSettingsSection appSettingSection = config.AppSettings; // Display raw xml.
Console.WriteLine(appSettingSection.SectionInformation.GetRawXml());
}
我记得好像是ConfigurationSettings.AppSettings["A"]<add key="A" value="config with A"/> 你试一下。
string myWebSiteName = ((NameValueCollection)ConfigurationSettings.GetConfig(""))["name"];