1

I'm busy creating an custom uninstall application.

I would like to know how do i delete my application programmaticly after it has run. I'm using a standard winform app coded in c#

We have an application that uses the clickonce deployment. Now i wish to create an uninstall function for that. I do already have the uninstall working fine, however i need to delete the uninstall application as well. it should just be a single exe file that needs to be deleted, after it's done its thing.

I do not wish to have any remaining file left on the user's machine

Josef Van Zyl
  • 915
  • 3
  • 19
  • 43
  • How could we answer this without knowing more details? Such as how and where you are installing it, how it runs etc. What's more, why write you own install program when VS ships with a tool to do just that? I'm not saying you don't have a reason, but explaining it is important to getting a good answer. – David Heffernan Mar 08 '11 at 08:33
  • Can't fully understand your request: I used both NSIS and InnoSetup to create installers/uninstallers and both doesn't left any file after uninstall. –  Jun 06 '16 at 12:17
  • @ElmoDev001 Dude, this question is 5 years old..... Regardless clickonce deployment did not support programmatic uninstall at that stage so we had to manually do it, With another single exe app, then I deleted that. The issue wasn't the installer leaving files behind, but the exe i used to run the uninstall. – Josef Van Zyl Jun 07 '16 at 08:37
  • Sorry, I have casually seen your question but I did not notice the date. –  Jun 07 '16 at 11:44

3 Answers3

2

I think that your question is already asked here. You have to use the MoveFileEx API, which, when given a MOVEFILE_DELAY_UNTIL_REBOOT flag, will delete specified file on next system startup.

Here you have a sample:

internal enum MoveFileFlags
{
    MOVEFILE_REPLACE_EXISTING = 1,
    MOVEFILE_COPY_ALLOWED = 2,
    MOVEFILE_DELAY_UNTIL_REBOOT = 4,
    MOVEFILE_WRITE_THROUGH  = 8
}

[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll",EntryPoint="MoveFileEx")]
internal static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName,
MoveFileFlags dwFlags);

MoveFileEx(fileToDelete, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
Community
  • 1
  • 1
Jaime Oro
  • 9,899
  • 8
  • 31
  • 39
1

Use Directory.Delete();

  System.IO.Directory.Delete("full_Path_of_Folder_to_delete", true);
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
1

That is a bit of a chicken-egg problem, but as long a file is not locked you can use Directory.Delete as suggested. I suggest you use a tool that depends on the microsoft installer or simply use the publish option to make your C# application installable.

If you really want to make your own installer and uninstaller, have a look at an open source solution like this to see how they do it: http://nsis.sourceforge.net/Main_Page

Wouter Simons
  • 2,856
  • 1
  • 19
  • 15