简体   繁体   中英

DeleteSubKey of previous Application Version/s C#

My application writes user settings to the registry for easy retrieval but this is causing a little headache because it's writing them as version specific.

So each time I update my application it creates a new subkey pertaining to the new version number.

Settings are written to the registry as:

Application.UserAppDataRegistry.SetValue

I would like to find utilize a simple method to delete all the VERSION subkeys that do not match the current Application version.

At the moment and because my application is relatively new I have resorted to this, which does work but is rather untidy:

Registry.CurrentUser.DeleteSubKey("SOFTWARE\\COMPANY\\PRODUCT\\1.0.0.0", false);

Registry.CurrentUser.DeleteSubKey("SOFTWARE\\COMPANY\\PRODUCT\\1.0.10.0", false);

in the above code I have removed the COMPANY & PRODUCT info deliberately & my application is currently on version 1.0.20.0 so at app start the DeleteSubKey lines are run and they are every time the app is run.

I am thinking that something along the lines of using:

If(PreviousVersion != Application.ProductVersion)
{
Registry.CurrentUser.DeleteSubKey(PreviousVersion);
}  

but I am having trouble fleshing this out.

Plus it would be nice to have it only run the one time and not at every startup.

You can list subkeys and check if they're different from current version:

RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\COMPANY\\PRODUCT");
var keys = rk.GetSubKeyNames();

foreach (string s in keys)
{
   if (s != "Current_verion_number")
   {
      Registry.CurrentUser.DeleteSubKeyTree("SOFTWARE\\COMPANY\\PRODUCT\\" + s);
   }       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM