2012-01-27 274 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在你的項目的引用,才能使用此。

相關問題