Nis*_*sim 1 c# file-io filehandle
在我的应用程序中附带一个卸载程序.一切都运行正常,除了在完成所有操作后我找不到删除uninstaller.exe文件的方法.
我试图将当前程序集exe复制到临时目录中,但原始文件的文件句柄仍然被锁定.
有任何想法吗?
你需要PInvoke才能做到这一点.MoveFileEx能够在下次重新启动时安排删除文件.
如果dwFlags指定MOVEFILE_DELAY_UNTIL_REBOOT且lpNewFileName为NULL,则MoveFileEx会在系统重新启动时注册要删除的lpExistingFileName文件.
就像是:
[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
}
Run Code Online (Sandbox Code Playgroud)