Reputation: 177
I have a C# application that store users settings in the standard user.config file.
This is obviously located in a directory named as the version number of the application i.e.
My_App/1.0.0.0/user.config
Having successfully upgraded to a new version of my application (1.0.0.1), the setting from v1.0.0.0 are copied and saved to the new version 1.0.0.1. That's the easy part.
Now I want to delete this previous version directory 1.0.0.0 after the upgrade.
I realize that the path to the directory cannot be hard coded as location varies on environment.
I have been looking to use:
using System.Configuration;
...
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
MessageBox.Show("Local user config path: {0}", config.FilePath);
straight away it throws an error and I need to add:
using EnvDTE;
now I have errors:
CS0117 'ConfigurationManager' does not contain a definition for 'OpenExeConfiguration'
CS0103 The name 'ConfigurationUserLevel' does not exist in the current context
CS1061 'Configuration' does not contain a definition for 'FilePath' and no extension method 'FilePath' accepting a first argument of type 'Configuration' could be found (are you missing a using directive or an assembly reference?)
Is there a way around this?
Otherwise at each update a new directory is going to be created and while each one is only 1kb, it seems ridiculous to let them all mount up.
Also, my application is not installed on the users system, it's all run directly from the distributed .exe
UPDATE 1
ADDING ERRORS WITH using EnvDTE; REMOVED as per comment.
CS0246 The type or namespace name 'Configuration' could not be found (are you missing a using directive or an assembly reference?)
CS0103 The name 'ConfigurationManager' does not exist in the current context
CS0103 The name 'ConfigurationUserLevel' does not exist in the current context
UPDATE 2
I thought I was getting somewhere by using:
string path = Application.LocalUserAppDataPath;
MessageBox.Show(path);
instead but this just creates a new path along side my original with an empty <Program Version>
directory in the form of:
<Company Name>/<Program Name>/<Program Version>
In the actual user.config
path the <Program Name>
has a hash string added to it to form something similar to:
<Program Name.exe_Url_aqod5o1hapcyjk3ku1gpyynhem0mefle>
I would assume that this hash is not a constant and does vary between machines & installs
UPDATE 3
Finally I have been able to retrieve the exact path to the user,config
file like this:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
MessageBox.Show(config.FilePath);
the using System.Configuration
was not working and I didn't realize that it was displayed in the code in a slightly duller colour because it was not referenced, so I added it as a reference by right clicking on the solution in explorer.
Now config
contains the full path to the user.config
file as in my case running Windows 10:
<DRIVE LETTER>\Users\<USER NAME>\AppData\Local\<Company Name>\<Program Name>\<Program Version>\user.config
I need to manipulate the config
string and climb out of the <Program Version>
directory & up to the <Program Name>
directory to check to see if there are any directories under it other than the current <Program Version>
directory and if there are, I need to delete them.
UPDATE 4
OK, really getting somewhere now. I have manipulated the filePath string in order to be at the level of <Program Version>
parent and using this code:
string FullfilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
string ProgNamefilePath = Path.GetFullPath(Path.Combine(FullfilePath, @"..\..\"));
Directory.Delete(ProgNamefilePath, true);
//MessageBox.Show(ProgNamefilePath);//For Testing Purposes
I can delete all directories under this, great.
No I'm stuck as I do not want to delete all the directories pertaining to <Program Version>
but only the one that do not match the current version.
Application.ProductVersion;
Upvotes: 4
Views: 2635
Reputation: 11
This is how i did it. I delete all user.config folders except the actual one after i did the upgrade. Therefore i don't need to manually add the current Version on the top. Hope that helps. I know it's a bit older the post, but it may help somebody. In my application i do this in 3 parts to make it work good.
a) global variable on UI
bool DeleteOldDirectories = false;
b) In my Main(), before UI i upgrade the properties and save them in my new Version
if (Settings.Default.UpgradeRequired)
{
Settings.Default.Upgrade();
Settings.Default.UpgradeRequired = false;
Settings.Default.Save();
DeleteOldDirectories = true;
}
In the Window.Loaded Method, after my UI is loaded and i'm sure all properties are updated correctly, i do delete the old directories, so all changes from now on are saved with my latest version and all reference to old Versions are gone.
if (deleteOldDirectories)
{
string FullfilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
string VersionDirectoryfilePath = System.IO.Path.GetFullPath(System.IO.Path.Combine(FullfilePath, @"..\..\"));
string[] fileEntries = Directory.GetDirectories(VersionDirectoryfilePath);
foreach (string filename in fileEntries)
{
// MessageBox.Show(filename);
if (filename != VersionDirectoryfilePath + System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString())
Directory.Delete(filename, true);
}
}
Upvotes: 1
Reputation: 177
A sleepless night thinking about this and trying to consider other avenues, I finally came up with this and it works.
There are probably neater ways to do this but anyway....
As high up in the code that I could possibly place it, so as to hopefully make it extremely obvious to myself when coding upgrades, I placed this:
public class MainForm : Form
{
//***********************************
//***********************************
//**** THIS RELEASE IS v1.0.0.1 ****
//***********************************
//****** REMEMBER TO EDIT THIS ******
//***********************************
const string DeleteThisVersionDirectory = "1.0.0.0"; //**** Set this to the <Program Version> Directory to be Deleted on Upgrade, usually version prior to new release. ****
//***********************************
//***********************************
you can see that it is directly under the Form
opening for visibility.
Then I created this Method:
private void ApplicationUpdate()
{
string FullfilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
string VersionDirectoryfilePath = Path.GetFullPath(Path.Combine(FullfilePath, @"..\..\" + DeleteThisVersionDirectory));
if (Properties.Settings.Default.UpdateSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpdateSettings = false;
Properties.Settings.Default.Save();
Directory.Delete(VersionDirectoryfilePath, true);
}
}
this Method is called here:
public MainForm()
{
InitializeComponent();
ApplicationUpdate();
GetSettingsValues();
}
It all works perfectly, so after a successful Upgrade
where user settings are transferred to the user.config
of the NEW Program Version, the previous Program Version directory and user.config
file is deleted.
I just have to remember to alter the const string DeleteThisVersionDirectory
on each version update coding.
Upvotes: 2