views:

98

answers:

3

In my application comes with an uninstaller Everything is working fine, except that I can't find no way to delete the uninstaller.exe file when it's all done.

I tried to copy the current assembly exe into a temp directory, but the file-handle of the original file is still locked...

Any ideas?

+1  A: 

It would be interesting if you posted some code of how you exactly copy the uninstaller.exe and change execution to that specific executable.
I think unloading the application domain will free the file-handle.

fretje
nothing fancy, simply copy the file to a temp directory (using File.Copy)starting it using Prcess.Start and disposing the originalWhat might help me is a way of starting an exe without the Process class (more to the PInvoke direction)
Nissim
+7  A: 

You will need to PInvoke to do this. MoveFileEx has the ability to schedule deleting the file on next reboot.

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file to be deleted when the system restarts.

Something like:

[return: MarshalAs (UnmanagedType.Bool)]
[DllImport ("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool MoveFileEx (string lpExistingFileName, string lpNewFileName, int dwFlags);

public static bool ScheduleDelete (string fileFullName) {
    if (!File.Exists (fileFullName))
        throw new InvalidOperationException ("File does not exist.");

    return MoveFileEx (fileFullName, null, 0x04); //MOVEFILE_DELAY_UNTIL_REBOOT = 0x04
}
Hemant
Thanks! works great... never would've think of this by myself but is there's anyway of deleting it right away? I know there are uninstallers which does exactly that!
Nissim
Perhaps the uninstaller which does that are executed by some other engine rather than being the engine itself. For example, if you have a msi, it is executed by windows installer, which in effect a different application, so it can delete the msi. But in your case, it is the engine itself. Deleting a locked file (any file, not just exe) will perhaps be file system stuff...
Hemant
+1  A: 

You might be able to achieve what you want by using shadow copying of assemblies, but I haven't tried that for this scenario.

Lucero