How do I share user settings or files using the UserAppDataPath across builds?

620 views Asked by At

Right now when I release a new build of my .NET app, the UserAppDataPath path points to a new folder that includes the build number.

Documents and Settings\UserName\Application Data\Company\AssemblyName\1.0.0.0

I use this path as a convenient storage place for extended user UI setting files. Every time I release the user looses their previous UI settings.

Is it safe to trim off the \1.0.0.0 version number and use its root path? or is there a better way to store settings in a place that is always has write privileges?

2

There are 2 answers

2
Nader Shirazie On BEST ANSWER

AppSettings do support upgrades. Have a look here. Hopefully this points you in the right direction...

0
Thomas853 On

I am using the following code when retrieving custom data stored in potentially old folders from previous assembly versions:

    string suffix = "/MyUserSettings.dat";
    string folder = Application.UserAppDataPath;
    string filename = folder + suffix;

    if (!File.Exists(filename))
    {
        // Check whether an older folder from a previous version with appropriate user data exists
        DirectoryInfo[] directories = new DirectoryInfo(folder).Parent.GetDirectories("*", SearchOption.TopDirectoryOnly);
        for (int i = 0; i < directories.Length; i++)
        {
            if (File.Exists(directories[i].FullName + suffix))
            {
                filename = directories[i].FullName + suffix;
            }
        }
    }

    if (File.Exists(filename))
    {
        // load user settings from file
    }
    else
    {
        // use default settings
    }