views:

314

answers:

2

Hello,

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?
A: 

Just to 'close' the question: The somewhat unsatisfactory solution I ended up with was

  1. Create a custom settings provider, which inherits from SettingsProvider and stores the settings in a XML file
  2. Set the Provider property of each of the setting (by selecting the entire grid in the designer) to the custom settings provider using the designer

Drawbacks: The forms designer breaks and gives an exception which basically says that the custom provider class cannot be found. The built exe however works OK. Setting the provider in the code as described in the question makes the designer work, but then for some reason, which I haven't looked closely at, the settings won't serialize.

It seems that making settings portable was all that was needed to make Doppler portable. Whether I'll start using Doppler as my main podcast aggregator or stick with my homebrew command line aggregator, I'll see.

Ville Koskinen
+1  A: 

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