【问题标题】:Can not move file in application and kill proces无法在应用程序中移动文件并终止进程
【发布时间】:2015-12-16 05:12:58
【问题描述】:

我使用 winrar 在我的应用程序中提取 rar 文件。在那个提取物竞争之后,我需要 移动提取的文件,但有时会出现此错误: 该进程无法访问该文件,因为它正被另一个进程使用

我猜这个错误是关于 winrar 所以我杀死了 winrar 进程但错误存在

我使用 (Process.HasExited) 强制程序休眠 5 分钟我可以杀死进程但有以下问题: 但是该进程无法访问该文件,因为它正在被另一个进程使用 并使用代码杀死进程:

 try
            {
                Process proc = Process.GetProcessById(id);
                proc.Kill();
            }
            catch (Exception ex)
            {
                Process[] processlist = Process.GetProcesses();
                foreach (Process p in Process.GetProcesses("."))
                {
                    try
                    {
                        if (p.ProcessName.ToLower().Contains("winrar"))
                        {
                            string str =            Bussiness.ClsCommandLine.GetCommandLine(p);
                            if (str.ToLower().Contains((fileName.ToLower()))
                            {
                                Process prc = Process.GetProcessById(p.Id);
                                prc.Kill();
                            }
                        }
                    }
                    catch (Exception ex1)
                    {
                        result = false;
                    }
                }
            }

有什么想法吗?

【问题讨论】:

    标签: c# kill-process


    【解决方案1】:

    无法评论,所以我必须添加作为答案:

    (假设您使用的是 win vista 以后)使用资源监视器查看关联的句柄(资源监视器 -> CPU -> 选中所有复选框 -> 关联的句柄)并查找具有句柄的任何进程(图像)您尝试移动的文件

    例如,图像 “winrar.exe” 可能有“c:\users\~username\my documents\directory\uncompressedfile.file”

    这应该让您了解哪些文件持有您尝试读取/修改/的文件

    【讨论】:

    • 使用代码杀死进程:Process proc = Process.GetProcessById(id); proc.Kill();
    • 和异常使用 :Process[] processlist = Process.GetProcesses(); foreach (Process.GetProcesses(".")中的进程 p) { if (p.ProcessName.Contains("Winrar")) { string str = Bussiness.ClsCommandLine.GetCommandLine(p); if (str.Contains(fileName) { Process prc = Process.GetProcessById(p.Id); prc.Kill(); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多