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?
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
.
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:
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>
You need the using statement:
using System.Configuration;
Besides these two, you also need a reference to the assembly.
The message
ConfigurationManager does not exist in the current context.
should be gone and you have your file path!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With