gpt4 book ai didi

c# - 无法删除正在使用的文件夹。

转载 作者:行者123 更新时间:2023-11-30 22:11:07 38 4
gpt4 key购买 nike

我尝试制作的自动更新软件有问题。该应用程序作为服务器应用程序运行,并通过 FTP 检查更新并在有更新版本可用时下载它们。然后在程序根目录中解压缩一个名为 update 的文件夹。然后它会启动一个名为 update.bat 的文件,该文件执行我可能需要为该更新执行的任何文件复制等操作。完成后,update.bat 将启动新的服务器应用程序。一旦程序再次检查更新,它就会删除服务器应用程序根目录中的更新目录以及从更新服务器下载的 update.rar 文件。除了文件夹正在使用并且不会删除之外,所有这些都可以正常工作。我已经阅读了关于释放句柄和更改当前目录等的各种内容。但似乎无法让它工作。我很感激有人在这里帮助我。这是此更新的代码。

        private void Form1_Load(object sender, EventArgs e)
{
foreach (string s in Directory.GetDirectories("C:/my update dir"))
{

if (s.Contains("Instance"))
{

var _instance = Regex.Match(s, @"\d+");

Process p = new Process();
ProcessStartInfo pinfo = new ProcessStartInfo();
pinfo.FileName = "cmd.exe";
pinfo.WorkingDirectory = "C:/mySQL/bin";
pinfo.Arguments = "/C mysql.exe -u** -p** dbnameHere" + _instance.ToString() +
" < \"C:/my update dir/update/update.sql\"";
p.StartInfo = pinfo;
p.Start();
p.WaitForExit();
p.Close();
p.Dispose();
Directory.SetCurrentDirectory("C:/");
}
}

Directory.SetCurrentDirectory("C:/");
this.Dispose();
Application.Exit();
}

最佳答案

我猜你的问题出在这里:

Once this is finished the update.bat launches the new server application.

Windows 将一直“锁定”目录直至 .BAT 文件。因此,如果 .BAT 从该目录运行服务器进程,该进程将继承调用进程的 CWD 和文件描述符。

从您的代码中不清楚这是正在发生的事情,但您可能还想在生成进程之前尝试更改 CWD。

关于c# - 无法删除正在使用的文件夹。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450435/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com