1

I have written a x64 windows installer for a C# windows service. The windows service itself targets Any CPU. It installs the service correctly to "Program Files". However, when uninstalling, I need to remove the logs directory. But when I try to obtain the path to "Program Files" using Environment ,it returns "Program Files (x86)", and hence fails to find the logs directory and delete it.How do I get around this. The code runs in the ProjectInstaller class and is as below

   try
    {
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\PathToLogsFolder"; 
logger.Info(path);
//The above line always shows Program Files (x86) instead of "Program Files"
    if (Directory.Exists(path))
    {

       Directory.Delete(path, true);

    }
    else
    {
      logger.Info("Path does not exist:"+path);
    }                
    }
    catch (Exception e)
    {
     logger.Error("Failed to delete Logs directory on uninstall:" + e.Message);
     logger.Error(e.StackTrace);
    }
Sagar Kapadia
  • 105
  • 1
  • 9
  • What tool are you using? You should be able to remove files using the built-in RemoveFile table in your MSI. Then there is no code to write at all. Moreover I prefer to not delete log files during uninstall, but instead document to the user how to clean up or better yet log to the system's event log and not to a file at all. You could log to a file in debug mode if you would like, but not for the release binary? – Stein Åsmul Oct 22 '18 at 14:41
  • Thanks Stein. I am using the windows installer Addin for VS 2017. You are right, I should use the RemoveFile table. I also write messages during uninstall to the systems event log, and that is how I figured that I am getting "Program Files (x86)" instead of "Program Files". The log files include information from previous runs, and I cant put everything in the system event log, hence I use log files. However, I would still like to have the issue resolved, if possible. But I will definitely check out the RemoveFiles table – Sagar Kapadia Oct 23 '18 at 12:45

1 Answers1

1

The most likely explanation is that your code is running as 32-bit code, not as native 64-bit code. You have a 64-bit installer but that does not mean that all your code will run 64-bit. You don't explicitly say this, but if that removal code is in a custom action then build that code to be explicitly 64-bit, and the same with the service.

PhilDW
  • 20,260
  • 1
  • 18
  • 28
  • Thanks Phil. The code is running in the windows Service, which targets "Any CPU". I couldn't get a x64 service to run on windows 10 for some reason. Should it not run as 64 bit, because the OS is 64 bit – Sagar Kapadia Oct 26 '18 at 13:28