C# – Delete Files/Folders under Program Files in Windows 7 / Vista

cnetwindowswindows 7windows-vista

We are rewriting our app to make it Windows Vista/7 compliant. In order to do that, I need to delete all the previous data in the application directory under %PROGRAMFILES%. This definitely will not work in user mode, and also doesn't appear to work programmatically in admin mode. I am trying to use the following line of code:

File.Delete(filePath);

Is there a way to write code to delete files in both user and admin mode? Going forward we will store all data under the %ALLUSERSPROFILE% directory, but until then I need to write migration code to handle this for the user.

Best Answer

No. You have to start the application as an administrator in order to get write access to %PROGRAMFILES%. In addition, if UAC is on, you may still get prompted for permission to continue even when running as an administrator.

As C-Pound Guru said in the comment to your question, your update installer (which should be run by an administrator) should also do the cleanup of old files.