2012-05-31 95 views
2

我如何從web.config使用c#代碼訪問文件夾路徑。 這裏是一個示例代碼。如何使用c#訪問web配置文件夾路徑#

如何放置在web配置C:\\whatever\\Data\\sample.xml

這條路我要讀從web配置這條道路。

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

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

請幫助.....

回答

7

下面是一些示例代碼,應該幫助你

這進入你的web.config。

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

這就是你怎麼讀它:

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

配置文件。

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

訪問它的代碼。

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

我建議你創建一個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]; 
相關問題