如何设置目录路径的默认值作为项目设置
问题描述:
在一个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
答
好,如果HomeDir
不Settings
设置(或路径不存在),使用方法:
string docs = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
获得Documents
文件夹的完整路径
请阅读我的问题的更新,我就可以用一个常数的默认值 – Ahmad
@Ahmad,我不认为你应该把默认值的配置,因为没有很好的默认用户'Documents'。离开它'“”'例如在代码中使用'HomeDir'值之前,检查它是否为空或存在,如果没有:然后去'System.Environment.GetFolderPath(...)' – ASh
谢谢,现在我得到了什么你意思是!我必须检查它是否为空。 – Ahmad