Search code examples
c#winformssettingsdefault

How to set default value of a directory path as a project setting


In a winform C# project, I added HomeDir as a directory path in the project settings. I want to set its initial value to Documents folder. This directory is not a constant string, so I can't use it in the setting dialog and also in the Settings.Designer.cs something like :

[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;
        }
    }

It will give the following error:

Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type


Solution

  • well, if HomeDir is not set in Settings (or path doesn't exist), use:

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

    to get Documents folder full path