如何获取当前用户的应用设置文件夹路径?

时间:2018-02-20 08:56:44

标签: c# wpf

当我使用代码时

Properties.Settings.Default.Save();

保存我的设置,将文件写入该路径

%USERPROFILE%\AppData\Local\MyAppName\MyAppName.exe_Url_SomeWiredCode\TheAppVersionNumber\user.config

"C:\Users\Username\AppData\Local\MyApp\MyApp.exe_Url_claumreyuxtgqul2vuc3couyu5tso2n0\1.0.0.0\user.config"

如何获取“app version”文件夹的路径,以便我可以编写其他内容?

我正在使用.Net Framework 4.6.2和Visual Studio 2017。

2 个答案:

答案 0 :(得分:3)

您应该能够像这样检索文件路径:

var level = ConfigurationUserLevel.PerUserRoamingAndLocal;
var configuration = System.Configuration.ConfigurationManager.OpenExeConfiguration(level);
var configurationFilePath = configuration.FilePath

要实现这一目标,您需要添加对System.Configuration.dll的引用(右键单击您的WPF项目,选择添加参考... 并检查程序集/框架页面中System.Configuration旁边的框。

答案 1 :(得分:0)

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

这将返回一个转到AppData的字符串。 只需将文件路径的其余部分添加到其末尾即可。