Delete user.config on uninstall through msiexec /x command line option

432 Views Asked by At

I have an MSI installer package that install the .net application. It also installs the Uninstall.bat file that does the uninstallation through the msiexec/x option. But it does not delete the user.config file created in the users Local Settings\Application Data folder\Company Name\Product folder.

I am not sure as to how do I do that as I am not able to figure out any option in the Installer itself.

I can create another batch file that can do the job but not sure if this would be the right way. Also, if I try doing it with Batch file, how would I find out the current user to reach to the correct folder? I don't want to make it too complicated. Is there an easy way of doing it.

Please suggest.

1

There are 1 best solutions below

0
On BEST ANSWER

I got it to work. I created a new Console project 'UninstallHelper' in the Solution. Added an InstallerClass in the UninstallHelper project. I am overriding the OnBeforeUninstall method:

protected override void OnBeforeUninstall(IDictionary savedState)
    {
        base.OnBeforeUninstall(savedState);
        try 
        {
            List<string> appFolders = new List<string>();
            string userFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string userDataFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);


            userFolderPath = userFolderPath + @"\CompanyName";
            userDataFolderPath = userDataFolderPath + @"\CompanyName";


            appFolders.Add(userFolderPath);
            appFolders.Add(userDataFolderPath);


            foreach (string folderPath in appFolders)
            {
                List<string> umDirs = new List<string>();
                DirectoryInfo targetDir = new DirectoryInfo(folderPath);

                foreach (DirectoryInfo dir in targetDir.GetDirectories())
                {
                    if (dir.Name.StartsWith("ProductName"))
                        umDirs.Add(dir.FullName);
                }

                foreach (string dirName in umDirs)
                {
                    DirectoryInfo subDir = new DirectoryInfo(dirName);
                    foreach (FileInfo file in subDir.GetFiles())
                    {
                        if (file.Exists)
                            file.Delete();
                    }

                    foreach (DirectoryInfo dir in subDir.GetDirectories())
                    {
                        if (dir.Exists)
                            dir.Delete(true);
                    }

                    subDir.Delete();
                }
            }
        }
        catch(Exception ex)
        {
            //Console.WriteLine(ex.Message);
            //Console.ReadKey();
        }

Now I add the Primary OutPut of this project in the installer. And finally, I add a Custom Action in Uninstall as the Primary Output from UninstallHelper project.

This did the trick for me.