文件路径定义的自定义 Web.config 部分
Custom Web.config section for file path definitions
我有一个将文件写入目录的应用程序。这个目录在我的 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>
不要使用 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))
我有一个将文件写入目录的应用程序。这个目录在我的 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>
不要使用 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))