2015-11-02 111 views
1

在一個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

回答

3

好,如果HomeDirSettings設置(或路徑不存在),使用方法:

string docs = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); 

獲得Documents文件夾的完整路徑

+0

請閱讀我的問題的更新,我就可以用一個常數的默認值 – Ahmad

+1

@Ahmad,我不認爲你應該把默認值的配置,因爲沒有很好的默認用戶'Documents'。離開它'「」'例如在代碼中使用'HomeDir'值之前,檢查它是否爲空或存在,如果沒有:然後去'System.Environment.GetFolderPath(...)' – ASh

+0

謝謝,現在我得到了什麼你意思是!我必須檢查它是否爲空。 – Ahmad