如何将目录路径的默认值设置为项目设置
How to set default value of a directory path as a project setting
在一个winform C#项目中,我在项目设置中添加了HomeDir
作为目录路径。我想将其初始值设置为 Documents
文件夹。这个目录不是常量字符串,所以我不能在设置对话框中使用它,也不能在 Settings.Designer.cs
中使用它,比如 :
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments))]
public string HomeDir
{
get
{
return ((string)(this["HomeDir"]));
}
set
{
this["HomeDir"] = value;
}
}
会报如下错误:
Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
嗯,如果Settings
中没有设置HomeDir
(或者路径不存在),使用:
string docs = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
获取Documents
文件夹完整路径
在一个winform C#项目中,我在项目设置中添加了HomeDir
作为目录路径。我想将其初始值设置为 Documents
文件夹。这个目录不是常量字符串,所以我不能在设置对话框中使用它,也不能在 Settings.Designer.cs
中使用它,比如 :
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments))]
public string HomeDir
{
get
{
return ((string)(this["HomeDir"]));
}
set
{
this["HomeDir"] = value;
}
}
会报如下错误:
Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
嗯,如果Settings
中没有设置HomeDir
(或者路径不存在),使用:
string docs = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
获取Documents
文件夹完整路径