Raj
Raj

Reputation: 487

How to access the folder path in web config using c#

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.....

Upvotes: 3

Views: 28659

Answers (3)

Saeed Neamati
Saeed Neamati

Reputation: 35852

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

Upvotes: 0

meetjaydeep
meetjaydeep

Reputation: 1850

Config file.

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

code to access it.

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

Upvotes: 1

nunespascal
nunespascal

Reputation: 17724

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();

Upvotes: 11

Related Questions