开发者

Delete application files after it runs

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


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


Use Directory.Delete();

  System.IO.Directory.Delete("full_Path_of_Folder_to_delete", true);


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜