How may I store a file path in my program's app.config file?

veronika.np picture veronika.np · Dec 11, 2012 · Viewed 47.3k times · Source

I have written a C# program for saving and reading PDF files. The program saves the output files to the local computer's bin folder. I want my program to access files from a different computer.

I have heard about keeping a file path stored in the app.config of my program, but I don't know how to do this.

How may I store a file path in my program's app.config file?

Answer

Scott Chapman picture Scott Chapman · Dec 11, 2012

You can store the file path in an app.config file by using this format in the app config:

<configuration>
 <appSettings>
  <add key="Path" value="\\ComputerName\ShareName"/>
 </appSettings>
</configuration>

You can then read the app settings stored there using the ConfigurationManager class. You'll have to add a reference to System.Configuration in your project, and reference it in the code.

After that, your path can be read by accessing ConfigurationManager.AppSettings["Path"] - it will be a string.