0

I have created installer which needs to delete some files and folder if they already exists and replace with new one.

When the user clicks on install, the installer prompts for UAC. On selecting Yes, the installer will call the method to delete existing files and folder. But it is unable to delete the files and folder. Code I am using is DI.Delete(path). It gives this error

Access to the path 'CodeCreate.aspx' is denied.
   at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
   at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
   at System.IO.DirectoryInfo.Delete(Boolean recursive)
   at CustomizationCA.CustomActions.DeleteExistingFilesAndFolder(Session session)

While if I run command prompt as administrator and then using "msiexec /i Setup.msi" command, it deletes those files and folder.

And when using Directory.Delete(path,true); I am getting this as exception

Exception thrown by custom action:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Security.SecurityException: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.
   at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly)
   at System.Diagnostics.EventLog.SourceExists(String source, String machineName)
   at System.Diagnostics.EventLog.VerifyAndCreateSource(String sourceName, String currentMachineName)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte rawData)
   at System.Diagnostics.EventLog.WriteEntry(String source, String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte rawData)
   at System.Diagnostics.EventLog.WriteEntry(String source, String message)
   at CustomizationCA.CustomActions.DeleteExistingFilesAndFolder(Session session)

I am using WIX tool to create installer.

If I go to properties of particular file and then give full permission to all users and then try to delete the file, the file is getting deleted. enter image description here

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
Sunil Agarwal
  • 4,097
  • 5
  • 44
  • 80

3 Answers3

5

Check to see if the folder or file is read-only or not. Bring files to normal state first.

Use this code before deleting any and every file in your directory

File.SetAttribute(path, FileAttributes.Normal);
File.Delete(path);
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
2

The custom action you use to delete the files must be deferred custom action and must have impersonate flag set to 'no'. Otherwise, the custom action is run with privileges of the user who started MSI installation, without elevation applied.


Better consider using RemoveFolderEx element in WiX to handle file removal rather than Custom Action.

Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
0

I´m not sure if I understand you. You created a installer and it´s deleting some files. When the installer starts it prompts for UAC. And by deleting the files you get a SecurityException.

Are you sure that the User who executes the installer have the permission to delete the file. If he haven´t that permission it could not work, thats logical. And you said that it work if you give the user fullcontrol.

If you want that it work, you have to give the users the permission to delete those files.

(Under Special permissions you find the "delete" permission)

Andre Gross
  • 263
  • 2
  • 8