删除当前加载的程序集

发布于 2024-07-26 10:53:57 字数 136 浏览 8 评论 0原文

我的应用程序附带一个卸载程序。 一切工作正常,除了在完成后我找不到删除 uninstaller.exe 文件的方法。

我尝试将当前程序集 exe 复制到临时目录中,但原始文件的文件句柄仍然被锁定。

有任何想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓦然回首 2024-08-02 10:53:57

您将需要 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
}

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
}
倾`听者〃 2024-08-02 10:53:57

如果您发布了一些如何准确复制 uninstaller.exe 并将执行更改为该特定可执行文件的代码,那将会很有趣。
我认为卸载应用程序域将释放文件句柄。

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.

情何以堪。 2024-08-02 10:53:57

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

驱逐舰岛风号 2024-08-02 10:53:57

您可以延迟使用“cmd”:

    internal static void ExitAndDelete()
    {
        var f = Application.ExecutablePath;
        Process.Start(new ProcessStartInfo("CMD.exe", "/C timeout 2&del \"" + f + "\"") { WindowStyle = ProcessWindowStyle.Hidden });
    }

You can use "cmd" with delay:

    internal static void ExitAndDelete()
    {
        var f = Application.ExecutablePath;
        Process.Start(new ProcessStartInfo("CMD.exe", "/C timeout 2&del \"" + f + "\"") { WindowStyle = ProcessWindowStyle.Hidden });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文