C# 应用程序:应用程序启动器,can';t移动目录,它';正在被另一个进程使用

C# 应用程序:应用程序启动器,can';t移动目录,它';正在被另一个进程使用,c#,.net,C#,.net,我正在编写应用程序启动器,作为C#VS 2017中的窗口应用程序。目前,这段代码有问题: if (System.IO.Directory.Exists(extractPath)) { string[] files = System.IO.Directory.GetFiles(extractPath); string[] dirs = Directory.GetDirectories(extractPath);

我正在编写应用程序启动器,作为C#VS 2017中的窗口应用程序。目前,这段代码有问题:

 if (System.IO.Directory.Exists(extractPath))
        {
            string[] files = System.IO.Directory.GetFiles(extractPath);
            string[] dirs = Directory.GetDirectories(extractPath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                var fileName = System.IO.Path.GetFileName(s);
                var destFile = System.IO.Path.Combine(oldPath, fileName);
                System.IO.File.Move(s, destFile);
            }

            foreach (string dir in dirs)
            {
                //var dirSplit = dir.Split('\\');
                //var last = dirSplit.Last();
                //if (last != "Resources")
                //{
                    var fileName = System.IO.Path.GetFileName(dir);
                    var destFile = System.IO.Path.Combine(oldPath, fileName);
                    System.IO.Directory.Move(dir, destFile);
                //}
            }
        }
我越来越了解这个错误了 “该进程无法访问文件'XXX',因为另一进程正在使用该文件。”

我一直在寻找解决方案来修复它,在MSDN和StackOvervflow上发现了一些,但我的问题非常具体。我无法仅将一个目录移动到另一个目录,这是我的主应用程序的资源文件夹:

以下是我对问题具体原因的解释:

  • 从父目录移动其他文件时,我没有任何问题。仅当循环到达/Resources目录时才会发生错误

  • 一开始,我认为它是由VS Instance使用的,我在其中打开了主应用程序。在关闭VS和杀戮过程后,没有任何变化

  • 我已将整个项目复制并移动到另一个目录。不要在VS中打开它,也不要通过*.exe文件启动它,以确保任何进程都不会使用新复制目录中的任何文件

  • 最后,我重新启动了电脑

  • 我知道,当你尝试删除/移动文件时,这个错误很常见,但在我的例子中,我确信它只被我的launcher应用程序使用。下面是一个稍长的示例代码,显示我实际执行的文件操作:

    private void RozpakujRepo()
        {
            string oldPath = @"path\Debug Kopia\Old";
            string extractPath = @"path\Debug Kopia";
    
            var tempPath = @"path\ZipRepo\RexTempRepo.zip";
    
             if (System.IO.File.Exists(tempPath) == true)
              {
            System.IO.File.Delete(tempPath);
            }
    
         System.IO.Compression.ZipFile.CreateFromDirectory(extractPath, tempPath);
    
    
            if (System.IO.Directory.Exists(oldPath))
            {
                DeleteDirectory(oldPath);
            }
            if (!System.IO.Directory.Exists(oldPath))
            {
                System.IO.Directory.CreateDirectory(oldPath);
            }
    
            if (System.IO.Directory.Exists(extractPath))
            {
                string[] files = System.IO.Directory.GetFiles(extractPath);
                string[] dirs = Directory.GetDirectories(extractPath);
    
                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    var fileName = System.IO.Path.GetFileName(s);
                    var destFile = System.IO.Path.Combine(oldPath, fileName);
                    System.IO.File.Move(s, destFile);
                }
    
                foreach (string dir in dirs)
                {
                    //var dirSplit = dir.Split('\\');
                    //var last = dirSplit.Last();
                    //if (last != "Resources")
                    //{
                        var fileName = System.IO.Path.GetFileName(dir);
                        var destFile = System.IO.Path.Combine(oldPath, fileName);
                        System.IO.Directory.Move(dir, destFile);
                    //}
                }
            }
    
            string zipPath = @"path\ZipRepo\RexRepo.zip";
    
    
    
            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    
    现在,我的问题是:

  • 它是否与文件类型(.png、.ico、.bmp)相关

  • 这些资源文件正在像我的主应用程序中的.exe文件图标那样被使用,这是否与此有关?或者仅仅因为这些是资源文件

  • 还有什么是我遗漏的,还有什么会导致错误

  • 编辑:

    澄清: 有两个应用程序:

  • 主要应用

  • 启动器应用程序(用于启动主应用程序)


  • 资源文件夹是主应用程序/资源,我在进行应用程序版本更新时移动它

    问题似乎位于与/Resources目录不同的位置。实际上,问题出在/Old目录上,因为它导致了有限的重复

    如果它是应用程序本身的资源文件夹,那么它将是第2个,因为应用程序本身正在使用这些文件。你理解错了。有两个应用程序-主应用程序和启动器应用程序(用于启动主应用程序)。在这种情况下,这是主应用程序的resources文件夹;我以前遇到过这个错误,因为该文件夹是cmd/powershell窗口上的当前路径,或者在资源管理器中打开了该文件夹。如果您还没有尝试过,可以使用sysinternals套件中的process explorer,查看哪个进程具有文件夹句柄。@CodedBeard我将尝试使用process explorer跟踪它,谢谢!