How to make designer generated .Net application settings portable

10,053

Solution 1

Why not use the CodeProject PortableSettingsProvider solution as is (with a few minor changes) ? I have done so in my project (StreamRecorder.NET) with success.

Some comments on the project's page were useful:

And the code I ended up with:

    static void Main(string[] args)
    {
        if (args.Contains("-p") || args.Contains("--portable"))
        {
            MakePortable(Properties.Settings.Default);
            MakePortable(Properties.LastUsedSettings.Default);
            MakePortable(Properties.DefaultSettings.Default);
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm(args));
    }

    private static void MakePortable(ApplicationSettingsBase settings)
    {
        var portableSettingsProvider = 
            new PortableSettingsProvider(settings.GetType().Name + ".settings");
        settings.Providers.Add(portableSettingsProvider);
        foreach (System.Configuration.SettingsProperty prop in settings.Properties)
            prop.Provider = portableSettingsProvider;
        settings.Reload();
    }

Lastly I made these changes to the CP project:

string _fileName;
public PortableSettingsProvider(string fileName)
{
    _fileName = fileName;
}

public virtual string GetAppSettingsFilename()
{
    //Used to determine the filename to store the settings
    //return ApplicationName + ".settings";
    return _fileName;
}

Solution 2

I know this question is quite old already. I just want to share my own version of a portable settings provider which I published as nuget package here.

The usage is pretty simple:

// make the default settings class portable
PortableSettingsProvider.ApplyProvider(Properties.Settings.Default);

I also explained the basic strategy of this implementation at https://www.codeproject.com/Articles/1238550/Making-Application-Settings-Portable.

Share:
10,053
simejo
Author by

simejo

Updated on June 18, 2022

Comments

  • simejo
    simejo almost 2 years

    I've been looking at modifying the source of the Doppler podcast aggregator with the goal of being able to run the program directly from my mp3 player.

    Doppler stores application settings using a Visual Studio designer generated Settings class, which by default serializes user settings to the user's home directory. I'd like to change this so that all settings would be stored in the same directory as the exe.

    It seems that this would be possible by creating a custom provider class which inherits the SettingsProvider class. Has anyone created such a provider and would like to share code?

    Update: I was able to get a custom settings provider nearly working by using this MSDN sample, i.e. with simple inheritance. I was initially confused as Windows Forms designer stopped working until I did this trick suggested at Codeproject:

    internal sealed partial class Settings
    {
        private MySettingsProvider settingsprovider = new MySettingsProvider();
    
        public Settings()
        {
            foreach (SettingsProperty property in this.Properties)
            {
                property.Provider = settingsprovider;
            }
        ...
    

    The program still starts with window size 0;0 though.

    Anyone with any insight to this?

    • Why the need to assing the provider in runtime---instead of using attributes as suggested by MSDN?
    • Why the changes in how the default settings are passed to the application with the default settings provider vs. the custom one?
  • Giorgi
    Giorgi over 13 years
    There is a new comment at codeproject for solving winforms designer problem: codeproject.com/Messages/3562657/…. Basically you need to include namespace in the provider name for the designer to work.