Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Web.Config读取变量_C#_.net_Asp.net_Web Config - Fatal编程技术网

C# 从Web.Config读取变量

C# 从Web.Config读取变量,c#,.net,asp.net,web-config,C#,.net,Asp.net,Web Config,如何从web.config文件中添加和读取值?假设密钥包含在节点中: ConfigurationSettings.AppSettings["theKey"]; 至于“写作”——简单地说,不要。 web.config不是为此而设计的,如果您要不断更改某个值,请将其放入静态帮助器类中。我建议您不要从中修改web.config,因为每次更改时,它都会重新启动应用程序 但是,您可以使用System.Configuration.ConfigurationManager.AppSettings阅读web.

如何从web.config文件中添加和读取值?

假设密钥包含在
节点中:

ConfigurationSettings.AppSettings["theKey"];
至于“写作”——简单地说,不要。


web.config不是为此而设计的,如果您要不断更改某个值,请将其放入静态帮助器类中。

我建议您不要从中修改web.config,因为每次更改时,它都会重新启动应用程序


但是,您可以使用
System.Configuration.ConfigurationManager.AppSettings

阅读web.config,Ryan Farley在他的博客中有一篇关于这一点的精彩文章,包括不写回web.config文件的所有原因:
给定以下web.config:

<appSettings>
     <add key="ClientId" value="127605460617602"/>
     <add key="RedirectUrl" value="http://localhost:49548/Redirect.aspx"/>
</appSettings>

如果您需要基本信息,可以通过以下方式访问密钥:

string myKey = System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString();
string imageFolder = System.Configuration.ConfigurationManager.AppSettings["imageFolder"].ToString();
为了访问我的web配置键,我总是在应用程序中创建一个静态类。这意味着我可以在任何需要的地方访问它们,并且我的应用程序中没有使用字符串(如果它在web配置中发生更改,我必须经历所有更改它们的事件)。以下是一个示例:

using System.Configuration;

public static class AppSettingsGet
{    
    public static string myKey
    {
        get { return ConfigurationManager.AppSettings["myKey"].ToString(); }
    }

    public static string imageFolder
    {
        get { return ConfigurationManager.AppSettings["imageFolder"].ToString(); }
    }

    // I also get my connection string from here
    public static string ConnectionString
    {
       get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }
    }
}

我是siteConfiguration类,可以这样调用我的所有应用程序设置。如果这对任何人都有帮助,我也会分享

在“web.config”中添加以下代码

现在添加上一个类的引用并访问一个键调用,如bellow

string appKeyStringVal= SiteConfigurationReader.appKeyString;
int appKeyIntVal= SiteConfigurationReader.appKeyInt;
int appKeyStringByPassingKey = SiteConfigurationReader.GetAppSettingsInteger("appKeyInt");

谢谢您,穆罕默德先生,那么您建议我如何在公共场所保存一个变量,该变量可以在不重新启动web应用程序的情况下进行更改?提前感谢您可以将这些变量存储在加密的XML文件中。是的,XML文件是更好的主意。或者,您可以将其存储在DB中,并添加到应用程序_start(Global.asax)中,将其放入应用程序变量中,然后在应用程序中使用这些变量。这些变量在应用程序中只分配一次,如果应用程序重新启动,这些变量将再次分配。非常感谢Vamyip先生和Muhammed先生的帮助+1个很好的答案。但是需要注意的是,您不需要显式调用
ToString
,因为
AppSettings
上的索引器返回类型
string
本身的值
<configuration>
   <configSections>
     <!-- some stuff omitted here -->
   </configSections>
   <appSettings>
      <add key="appKeyString" value="abc" />
      <add key="appKeyInt" value="123" />  
   </appSettings>
</configuration>
using System; 
using System.Configuration;
namespace Configuration
{
   public static class SiteConfigurationReader
   {
      public static String appKeyString  //for string type value
      {
         get
         {
            return ConfigurationManager.AppSettings.Get("appKeyString");
         }
      }

      public static Int32 appKeyInt  //to get integer value
      {
         get
         {
            return ConfigurationManager.AppSettings.Get("appKeyInt").ToInteger(true);
         }
      }

      // you can also get the app setting by passing the key
      public static Int32 GetAppSettingsInteger(string keyName)
      {
          try
          {
            return Convert.ToInt32(ConfigurationManager.AppSettings.Get(keyName));
        }
        catch
        {
            return 0;
        }
      }
   }
}
string appKeyStringVal= SiteConfigurationReader.appKeyString;
int appKeyIntVal= SiteConfigurationReader.appKeyInt;
int appKeyStringByPassingKey = SiteConfigurationReader.GetAppSettingsInteger("appKeyInt");