【问题标题】:How to access the folder path in web config using c#如何使用C#访问Web配置中的文件夹路径
【发布时间】:2012-05-31 13:42:21
【问题描述】:

如何使用 c# 代码从 web.config 访问文件夹路径。 这是一个示例代码。

如何将此路径放在网络配置C:\\whatever\\Data\\sample.xml

我必须从网络配置中读取此路径。

string folderpath = ConfigurationSettings.AppSettings["path"].ToString();

using (XmlWriter xw = XmlWriter.Create(folderpath, new XmlWriterSettings { Indent = false }))

请帮忙.....

【问题讨论】:

    标签: asp.net c#-4.0


    【解决方案1】:

    这里有一些可以帮助你的示例代码

    这会进入您的 web.config。

    <configuration>
        <appSettings>
            <add key="myFilePath" value="C:\\whatever\\Data\\sample.xml"/>
        </appSettings>
    </configuration>
    

    这就是你的阅读方式:

    path = System.Web.Configuration.WebConfigurationManager.AppSettings["myFilePath"].ToString();
    

    【讨论】:

      【解决方案2】:

      配置文件。

       <configuration>
            <appSettings>
              <add key="path" value="c:\dev"/>
            </appSettings>
          </configuration>
      

      访问它的代码。

       string path = System.Configuration.ConfigurationSettings.AppSettings["path"].ToString();
      

      【讨论】:

        【解决方案3】:

        我建议你创建一个SettingsManager 类,它有两个方法:

        public class SettingsManager
        {
            public string Get(string key) 
            {
               // Reading settings from anywhere, in your case from web.config;
            }
        
            public string Save(string key, string value)
            {
               // Saving settings in a storage, here in web.config; 
            }
        }
        

        然后从 web.config 中读取,只需使用 WebConfigurationManager 使用:

        return WebConfigurationManager.AppSettings[key];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-01-27
          • 1970-01-01
          • 2016-11-10
          • 1970-01-01
          • 2016-06-20
          • 2014-02-06
          • 1970-01-01
          相关资源
          最近更新 更多