C# 应用程序:应用程序启动器,can';t移动目录,它';正在被另一个进程使用
我正在编写应用程序启动器,作为C#VS 2017中的窗口应用程序。目前,这段代码有问题: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);
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上发现了一些,但我的问题非常具体。我无法仅将一个目录移动到另一个目录,这是我的主应用程序的资源文件夹:
以下是我对问题具体原因的解释:
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);
}
现在,我的问题是:
资源文件夹是主应用程序/资源,我在进行应用程序版本更新时移动它 问题似乎位于与/Resources目录不同的位置。实际上,问题出在/Old目录上,因为它导致了有限的重复 如果它是应用程序本身的资源文件夹,那么它将是第2个,因为应用程序本身正在使用这些文件。你理解错了。有两个应用程序-主应用程序和启动器应用程序(用于启动主应用程序)。在这种情况下,这是主应用程序的resources文件夹;我以前遇到过这个错误,因为该文件夹是cmd/powershell窗口上的当前路径,或者在资源管理器中打开了该文件夹。如果您还没有尝试过,可以使用sysinternals套件中的process explorer,查看哪个进程具有文件夹句柄。@CodedBeard我将尝试使用process explorer跟踪它,谢谢!