2012-01-27 55 views
9

我想在我的窗口来显示user.config文件的位置,形成了应用程序,使用户可以轻松地找到它。如何以编程方式获取user.config文件的位置?

我明白是怎么路径创建感谢:Can I control the location of .NET user settings to avoid losing settings on application upgrade?

然而,如果这个变化,我宁可不要构建路径这在我的应用程序,尤其是如果有用于获取user.config文件位置的简单方法。

回答

20

试试这个:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming); 

MessageBox.Show(config.FilePath); 
+2

这正是我需要的。任何想法ConfigurationUserLevel.PerUserRoamingAndLocal和ConfigurationUserLevel.PerUserRoaming之间的真正区别是什么? RoamindAndLocal似乎涵盖两种情况? – 2012-01-27 21:18:50

+0

要获得适用于当前用户的本地配置对象,用户级设置为PerUserRoamingAndLocal。 要获取适用于当前用户的漫游配置对象,请将userLevel设置为PerUserRoaming。 – 2016-07-11 15:43:51

4

取决于如何应用程序运行,ConfigurationUserLevel.PerUserRoamingAndLocal可能是你正在寻找的,而不是ConfigurationUserLevel.PerUserRoaming财产;

即:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 
MessageBox.Show(config.FilePath); 

一定有System.Configuration在你的项目的引用,才能使用此。

相关问题