When uninstalling a windows service with a x64 installer , Environment.SpecialFolder.ProgramFiles returns "Program Files (x86)"

70 Views Asked by At

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);
    }
1

There are 1 best solutions below

1
PhilDW On

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.