如何阅读web.config文件中的appSettings部分()

识字粗堪供赋役,不须辛苦慕公卿。这篇文章主要讲述如何阅读web.config文件中的appSettings部分?相关的知识,希望能为你提供帮助。
我的XML看起来像这样,文件名是web.config

< ?xml version="1.0"?> < configuration> < appSettings> < add key="configFile" value="https://www.songbingjia.com/android/IIS.config"/> < add key="RialtoDomain" value="https://www.songbingjia.com/android/ASNC_AUDITORS"/> < /appSettings> < system.serviceModel> .... < /system.serviceModel> < /configuration>

在我读这样的代码中
String path = ConfigurationSettings.AppSettings["configFile"];

我得到一个空值。没有异常被抛出。这是正确的方法吗?
答案因为你正在访问web.config,你应该使用
using System.Web.Configuration; WebConfigurationManager.AppSettings["configFile"]

另一答案添加命名空间
using System.Configuration;

代替
ConfigurationSettings.AppSettings
你应该使用
ConfigurationManager.AppSettings
String path = ConfigurationManager.AppSettings["configFile"];

另一答案
ConfigurationManager.AppSettings["configFile"]

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx
另一答案您应该添加System.configuration dll作为参考并使用System.Configuration.ConfigurationManager.AppSettings["configFile"].ToString
【如何阅读web.config文件中的appSettings部分()】不要忘记在开头添加usingstatement。希望它会有所帮助。
另一答案
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; }


    推荐阅读