0

All, I am doing the usual thing of writing application settings to the 'application.exe.config' file using

Properties.Settings.Default.SomeSetting = someVal;
Properties.Settings.Default.Save();

I have been asked to persist settings between installations and there are two routes; switch to using the registry, or save the .config file to a separate Special Folder that is persisted between installations (I have chosen the later due to number of settings).

My .config gets written to an odd directory, namely

C:\Users\Administrator\AppData\Local\MyApp\
    MyApp.vshost.exe_Url_mzfwtdo5po4pcuabybebhsn5yfltbb3w\1.0.0.0

My question is: how do I pick this directory up in C#?

Note: I have tried

string appPath = Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(appPath);
string strIPACostConfigFile = config.FilePath;

Which gives me the initial .config in the installation directory.

Thanks for your time.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 1
    Are you using a ClickOnce? Why not create an additional config/XML file that is stored in the user's local storage for your persisted settings? – zimdanen May 08 '12 at 14:57
  • Using NSIS as the installer. Is suppose there are many ways to do this. What I have at the moment handles all my settings needs, bar persistance between installation. Rather than create two config files, I just want to store my config file upon uninstalltion, this then gets copied to the the relevent directory the first time the application is used after a reinstallation. If there is a better way to do this I would very much appreciate advice... Thanks for your time. – MoonKnight May 08 '12 at 15:10
  • 2
    @Killercam, please look at the second answer from [this question](http://stackoverflow.com/questions/621265/can-i-control-the-location-of-net-user-settings-to-avoid-losing-settings-on-app) – Steve May 08 '12 at 15:17
  • 1
    Have a look at [LocalFileSettingsProvider.Upgrade Method](http://msdn.microsoft.com/en-us/library/system.configuration.localfilesettingsprovider.upgrade.aspx). – Igby Largeman May 08 '12 at 15:19
  • Thank you for that I had no idea that existed! However, I have used `Properties.Settings.Default.Upgrade()`, but now even when I take this out - the application still remembers the settings!? I am really confused as to what is going on here - does the Upgrade method write to the regestry? – MoonKnight May 08 '12 at 16:36

2 Answers2

1

My question is: how do I pick this directory up in C#?

You cannot. The App.exe.config file can be in one of two places, unless you load, generate, and save the configuration file yourself, you will be unable to locate it in the location you want.

Of course the location that Microsoft decided upon is the correct location for it

Security Hound
  • 2,577
  • 3
  • 25
  • 42
1

You don't have to know the location of your config file. You just need a Setting that is true by default and call the following call when your programm starts.

if (Settings.Default.IsUpgrade)
{
  Settings.Default.Upgrade();
  Settings.Default.IsUpgrade = false;
  Settings.Default.Save();
}

That way, settings made in an earlier version will be migrated to the new one.

shriek
  • 5,157
  • 2
  • 36
  • 42
  • Thank you for that I had no idea that existed! However, I have used Properties.Settings.Default.Upgrade(), but now even when I take this out - the application still remembers the settings!? I am really confused as to what is going on here - does the Upgrade method write to the registry? – MoonKnight May 08 '12 at 16:37
  • @Killercam: Of course it works after taking it out, however as soon as you change your version again the old settings will be gone. So just keep that bit of code in there and everything will work. – shriek May 08 '12 at 16:40
  • Ahh, I see. So it is using the registry... This is a good answer - although the others may have beaten you to it! :] – MoonKnight May 08 '12 at 16:44