如何在用户范围设置中设置默认应用程序路径值
在我的 C# 项目中,我有一个文件夹路径的用户范围设置,我想在设计时设置它,以便它成为新用户的默认值(如果我没有记错的话)。
我想将默认值设置为用户 AppData 文件夹之一。我应该在设置中键入什么值?当您在解决方案资源管理器中双击 MSVS Settings.settings UI(不确定它叫什么)时,我指的是 MSVS Settings.settings UI。
该值应该是由 Application.UserAppDataPath 返回的值
(请结合我的其他问题阅读:Environment.SpecialFolders 和 Application 文件夹之间的 C# 区别 我应该使用什么路径)
谢谢!
更新:
根据 shf301 的回答,我进入了 settings.designer.cs 并执行了以下操作:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string LogFolder {
get {
return ((string)(this["LogFolder"])) ?? System.Windows.Forms.Application.LocalUserAppDataPath;
}
set {
this["LogFolder"] = value;
}
}
In my C# project, I have a user scoped setting for a folder path which I want to set at design time so that it becomes the default value for new users (If i am not mistaken).
I want to set the default value to one of the user AppData folder. What do I key as the value in the settings? I am refering to the MSVS Settings.settings UI when you double click it in the Solution Explorer (not sure what it is called).
The value should be one that is returned by, for example, Application.UserAppDataPath
(Please read in conjunction with my other question: C# difference between Environment.SpecialFolders and Application folders as to what path I should use)
Thanks!
UPDATE:
With shf301's answer, I went inside settings.designer.cs and did this:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string LogFolder {
get {
return ((string)(this["LogFolder"])) ?? System.Windows.Forms.Application.LocalUserAppDataPath;
}
set {
this["LogFolder"] = value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需在设置中键入任何内容,因为您无法知道用户的 AppData 文件夹。将默认值保留为空,并在使用该设置的代码中,如果未设置该设置(null 或空字符串),则使用
Application.UserAppDataPath
否则使用用户设置。例如:
You don't key anything into the settings because you can't know the user's AppData folder. Leave the default value empty and in your code where you use the setting, if the setting is not set (a null or empty string) then use
Application.UserAppDataPath
otherwise use the users settings.For example: