升级winforms应用程序失败-进程无法访问文件'C:..WinForm.exe'因为它正在被另一

本文关键字:因为 exe WinForm 进程 访问 文件 winforms 升级 应用程序 失败 | 更新日期: 2023-09-27 17:54:57

我实现了一个功能来检查是否有新版本的Win Form应用程序。

如果有新版本,我会尝试复制所有的文件。包括可执行文件。

但是当我的功能试图复制可执行文件时,我得到错误消息

进程无法访问文件"C:…'WinForm.exe",因为它是正在被另一个进程使用

代码下面:

public void CopyAll(string sourceFolder, string destFolder)
{
    CopyOutput = new List<string>();
    if (!Directory.Exists(destFolder))
        Directory.CreateDirectory(destFolder);
    string[] files = Directory.GetFiles(sourceFolder);
    foreach (string file in files)
    {
        string name = Path.GetFileName(file);
        string dest = Path.Combine(destFolder, name);
        File.Copy(file, dest, true);
        CopyOutput.Add("File copied: " + dest);
    }
    string[] folders = Directory.GetDirectories(sourceFolder);
    foreach (string folder in folders)
    {
        string name = Path.GetFileName(folder);
        string dest = Path.Combine(destFolder, name);
        CopyAll(folder, dest);
    }
}

升级winforms应用程序失败-进程无法访问文件'C:..WinForm.exe'因为它正在被另一

你可以使用ClickOnce,或者如果这不能满足你的需要,你可以使用Visual Studio扩展来构建一个MSI安装程序:

  • Microsoft Visual Studio 2015 Installer Projects: https://visualstudiogallery.msdn.microsoft.com/f1cc3f3e-c300-40a7-8797-c509fb8933b9
  • Microsoft Visual Studio 2013 Installer Projects: https://visualstudiogallery.msdn.microsoft.com/9abe329c-9bba-44a1-be59-0fbf6151054d

我不建议您编写自己的安装程序-因为您已经发现有很多事情需要正确处理。

正如其他人所回应的那样,这可能不是解决问题的方法。但是要直接回答您的问题,正常的解决方案是将新的EXE/DLL文件复制到子文件夹,或到不同的文件名(即。"MyEXE.exe.new")。在退出当前应用程序实例之前,启动一个简单的。net实用程序应用程序,它复制"。然后,实用程序应用程序启动新刷新的。exe文件并退出。

通过非常小心地处理AppDomain,您可能可以省略实用程序应用程序,但将其作为两个应用程序处理要简单得多。