ver*_*.np 8 c# app-config filepath winapp
我编写了一个用于保存和阅读PDF文件的C#程序.程序将输出文件保存到本地计算机的bin文件夹.我希望我的程序从另一台计算机访问文件.
我听说保存文件路径存储在app.config我的程序中,但我不知道如何做到这一点.
如何在程序的app.config文件中存储文件路径?
Sco*_*man 31
您可以在app config中使用以下格式将文件路径存储在app.config文件中:
<configuration>
<appSettings>
<add key="Path" value="\\ComputerName\ShareName"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用ConfigurationManager类读取存储在那里的应用程序设置.您必须在项目中添加对System.Configuration的引用,并在代码中引用它.
之后,您可以通过访问来读取您的路径ConfigurationManager.AppSettings["Path"]- 它将是一个string.
小智 6
我是一个完整的菜鸟,但我最近遇到了同样的问题,所以这就是我想出的。
解决方案是三个单独的步骤:
如上面的答案所述,您添加一个键值app.config指向您的路径:
<configuration>
<appSettings>
<add key="Path" value="C:\text.txt"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
您需要使用语句:
using System.Configuration;
Run Code Online (Sandbox Code Playgroud)
除了这两个之外,您还需要对程序集的引用。
消息
当前上下文中不存在 ConfigurationManager。
应该消失了,你有你的文件路径!