16

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?

HappyCoding
  • 641
  • 16
  • 36
veronika.np
  • 361
  • 1
  • 4
  • 11
  • I think you asked same question before. what's the thing you are not getting? http://stackoverflow.com/questions/7606955/how-implement-usercontrol-in-winforms-mvp-pattern – RATHI Dec 11 '12 at 19:12
  • sorry I copy question wrong...I edit it – veronika.np Dec 11 '12 at 19:26

3 Answers3

36

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

<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.

Amal K
  • 4,359
  • 2
  • 22
  • 44
Scott Chapman
  • 920
  • 1
  • 7
  • 13
  • 1
    As addition to your answer, when you need to put path to folder in value it could be in format like: "C:\Temp\Folder" (no need to escape backslash sign like "C:\\Temp\\Folder") – Reven Nov 09 '17 at 13:30
7

I'm a complete noob but I recently had the same issue so here is what I came up with.

The solution is three separate steps:

  1. As stated in the answer above, you add a key value to app.config that points to your path:

     <configuration>
       <appSettings>
         <add key="Path" value="C:\text.txt"/>
       </appSettings>
     </configuration>
    
  2. You need the using statement:

    using System.Configuration;
    

Besides these two, you also need a reference to the assembly.

  1. Right click on References in Solution Explorer.
  2. Click Add Reference.
  3. Click on Assemblies and type Configuration in the search box.
  4. Put a check mark on System.Configuration.

The message

ConfigurationManager does not exist in the current context.

should be gone and you have your file path!

Amal K
  • 4,359
  • 2
  • 22
  • 44
CDGreeneyes
  • 71
  • 1
  • 2
0

First: Create or Add app.config to your solution:

<configuration>
   <appSettings>
     <add key="Path" value="C:\text.txt"/>
   </appSettings>
 </configuration>

Last: Inside your winForm wirte these code:

string str= System.Configuration.ConfigurationManager.AppSettings["Source"];
            MessageBox.Show(str);
MessageBox.Show(str);

That is allllll

Aso Salih
  • 1
  • 1