Question

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;
    }
}
Was it helpful?

Solution

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:

public static string GetUserPath()
{
    string path = Settings.Default.UserPath;
    if (string.IsNullOrEmpty(path))
        path = Application.UserAppDataPath;
    return path;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top