-2

I have an application that writes files to a directory. This directory is hard coded in my C#, for example...

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

I want to store the directory in the web config, and reference it from my C#. Every attempt I've made has resulted in either invalid xml or an error, so I was hoping for some help - thanks

I tried putting it in connectionStrings

<connectionStrings>
<add name = "exportppath1" value = "C:\BillingExport\EXPORTS\TRANSACTIONS\"/>
</connectionStrings>
  • "Every attempt I've made" well, you should tell us what you've done. That means providing code. – mason Jan 13 '15 at 19:06
  • http://stackoverflow.com/questions/16524594/reading-custom-configuration-sections-key-values-in-c-sharp – Leo Jan 13 '15 at 19:06
  • You tagged this as `sql` and `iis`...What's the direct connection with those tags here? – Claudio Redi Jan 13 '15 at 19:12
  • streamwriter is being used to collect data from a sql database, then write it to a file (as you can see in the code snippit), and web.config is interpreted by IIS and having invalid xml is causing my errors. – frank haverford Jan 13 '15 at 21:47

1 Answers1

1

Don't use connectionStrings since it make no sense on your scenario. That section is for database connection strings.

You should use appSettings section

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

And then on your code you would do

string exportDirectory = ConfigurationManager.AppSettings["exportDirectory"]
string exportFilePath = Path.Combine(exportDirectory, "TYPE07.txt");
using (StreamWriter writer = new StreamWriter(exportFilePath))
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155