How to access the folder path in web config using

2020-07-25 01:50发布

问题:

How can i access the folder path from web.config using c# code. here is a sample code.

how to place this path in web config C:\\whatever\\Data\\sample.xml

i have to read this path from web config.

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

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

please help.....

回答1:

Here is some sample code that should help you

This goes into your web.config.

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

And this is how you read it:

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


回答2:

Config file.

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

code to access it.

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


回答3:

I suggest you create a SettingsManager class, which has two methods:

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; 
    }
}

Then to read from web.config, simply use WebConfigurationManager using:

return WebConfigurationManager.AppSettings[key];