自定义Web.config部分,用于文件路径定义

时间:2015-01-13 18:57:16

标签: c# sql iis

我有一个将文件写入目录的应用程序。这个目录在我的C#中进行了硬编码,例如......

using (StreamWriter writer = new StreamWriter("C:\\BillingExport\\EXPORTS\\TRANSACTIONS\\TYPE07.txt"))

我想将目录存储在Web配置中,并从我的C#中引用它。我所做的每一次尝试都会导致xml无效或出错,所以我希望得到一些帮助 - 谢谢

我尝试将其放入connectionStrings

<connectionStrings>
<add name = "exportppath1" value = "C:\BillingExport\EXPORTS\TRANSACTIONS\"/>
</connectionStrings>

1 个答案:

答案 0 :(得分:1)

请勿使用connectionStrings,因为它对您的方案毫无意义。该部分用于数据库连接字符串。

您应该使用appSettings部分

<appSettings>
   <add key="exportDirectory" value="C:\BillingExport\EXPORTS\TRANSACTIONS" />
</appSettings>

然后在你的代码上你会做

string exportDirectory = ConfigurationManager.AppSettings["exportDirectory"]
string exportFilePath = Path.Combine(exportDirectory, "TYPE07.txt");
using (StreamWriter writer = new StreamWriter(exportFilePath))