My XML looks like this and the filename is web.config
我的XML是这样的,文件名是web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="configFile" value="IIS.config"/>
<add key="RialtoDomain" value="ASNC_AUDITORS"/>
</appSettings>
<system.serviceModel>
....
</system.serviceModel>
</configuration>
In the code when I read like this
在代码中,当我这样读的时候
String path = ConfigurationSettings.AppSettings["configFile"];
I am getting a null value. No exception is thrown. Is this the right way to do it?
我得到一个空值。没有异常。这是正确的做法吗?
5 个解决方案
#1
127
Since you're accessing a web.config
you should probably use
因为你正在访问一个web。配置您应该使用。
using System.Web.Configuration;
WebConfigurationManager.AppSettings["configFile"]
#2
26
Add namespace
添加名称空间
using System.Configuration;
and in place of
在的地方
ConfigurationSettings.AppSettings
ConfigurationSettings.AppSettings
you should use
你应该使用
ConfigurationManager.AppSettings
ConfigurationManager.AppSettings
String path = ConfigurationManager.AppSettings["configFile"];
#3
5
ConfigurationManager.AppSettings["configFile"]
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
#4
4
You should add System.configuration dll as reference and use System.Configuration.ConfigurationManager.AppSettings["configFile"].ToString
你应该添加系统。配置dll作为参考,并使用system.configurationmanager . appsettings ["configFile"].ToString。
Don't forget to add using
statement at the beginning. Hope it will help.
不要忘记在开头添加usingstatement。希望它会有所帮助。
#5
0
using System.Configuration;
/// <summary>
/// For read one setting
/// </summary>
/// <param name="key">Key correspondent a your setting</param>
/// <returns>Return the String contains the value to setting</returns>
public string ReadSetting(string key)
{
var appSettings = ConfigurationManager.AppSettings;
return appSettings[key] ?? string.Empty;
}
/// <summary>
/// Read all settings for output Dictionary<string,string>
/// </summary>
/// <returns>Return the Dictionary<string,string> contains all settings</returns>
public Dictionary<string, string> ReadAllSettings()
{
var result = new Dictionary<string, string>();
foreach (var key in ConfigurationManager.AppSettings.AllKeys)
result.Add(key, ConfigurationManager.AppSettings[key]);
return result;
}
#1
127
Since you're accessing a web.config
you should probably use
因为你正在访问一个web。配置您应该使用。
using System.Web.Configuration;
WebConfigurationManager.AppSettings["configFile"]
#2
26
Add namespace
添加名称空间
using System.Configuration;
and in place of
在的地方
ConfigurationSettings.AppSettings
ConfigurationSettings.AppSettings
you should use
你应该使用
ConfigurationManager.AppSettings
ConfigurationManager.AppSettings
String path = ConfigurationManager.AppSettings["configFile"];
#3
5
ConfigurationManager.AppSettings["configFile"]
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
#4
4
You should add System.configuration dll as reference and use System.Configuration.ConfigurationManager.AppSettings["configFile"].ToString
你应该添加系统。配置dll作为参考,并使用system.configurationmanager . appsettings ["configFile"].ToString。
Don't forget to add using
statement at the beginning. Hope it will help.
不要忘记在开头添加usingstatement。希望它会有所帮助。
#5
0
using System.Configuration;
/// <summary>
/// For read one setting
/// </summary>
/// <param name="key">Key correspondent a your setting</param>
/// <returns>Return the String contains the value to setting</returns>
public string ReadSetting(string key)
{
var appSettings = ConfigurationManager.AppSettings;
return appSettings[key] ?? string.Empty;
}
/// <summary>
/// Read all settings for output Dictionary<string,string>
/// </summary>
/// <returns>Return the Dictionary<string,string> contains all settings</returns>
public Dictionary<string, string> ReadAllSettings()
{
var result = new Dictionary<string, string>();
foreach (var key in ConfigurationManager.AppSettings.AllKeys)
result.Add(key, ConfigurationManager.AppSettings[key]);
return result;
}