我编写了一个用于保存和读取PDF文件的C#程序。该程序将输出文件保存到本地计算机的bin文件夹中。 我希望我的程序可以从不同的计算机访问文件。
我听说过在程序的app.config
中存储文件路径,但我不知道如何实现。
我该如何在程序的app.config文件中存储文件路径?
我编写了一个用于保存和读取PDF文件的C#程序。该程序将输出文件保存到本地计算机的bin文件夹中。 我希望我的程序可以从不同的计算机访问文件。
我听说过在程序的app.config
中存储文件路径,但我不知道如何实现。
我该如何在程序的app.config文件中存储文件路径?
app.config
文件中:
<configuration>
<appSettings>
<add key="Path" value="\\ComputerName\ShareName"/>
</appSettings>
</configuration>
ConfigurationManager
类读取存储在其中的应用程序设置。您需要在项目中添加对System.Configuration
的引用,并在代码中引用它。ConfigurationManager.AppSettings["Path"]
来读取路径 - 它将是一个string
。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;
<configuration>
<appSettings>
<add key="Path" value="C:\text.txt"/>
</appSettings>
</configuration>
string str= System.Configuration.ConfigurationManager.AppSettings["Source"];
MessageBox.Show(str);
MessageBox.Show(str);
那就是全部啦