Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make designer generated .Net application settings portable

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?
like image 740
Ville Koskinen Avatar asked Sep 05 '09 06:09

Ville Koskinen


People also ask

Where are .NET application settings stored?

Settings. settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there.

How do I create a settings file in Visual Studio?

In Visual Studio, from the Project menu, choose Add New Item. The Add New Item dialog box opens. In the Add New Item dialog box, select Settings File, enter a name for the file, and click Add to add a new settings file to your solution. In Solution Explorer, drag the new Settings file into the Properties folder.

How do I get to application settings?

From the Home screen, tap the Apps icon (in the QuickTap Bar) > the Apps tab (if necessary) > Settings .


2 Answers

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:

  • http://www.codeproject.com/Messages/2934144/Fixed-csharp-version.aspx
  • http://www.codeproject.com/Messages/3285411/Re-Win-Form-Designer-breaking-with-custom-Settings.aspx

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;
}
like image 73
Ohad Schneider Avatar answered Sep 30 '22 18:09

Ohad Schneider


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.

like image 30
alxnull Avatar answered Sep 30 '22 18:09

alxnull