c# 使用handle.exe解决程序更新文件被占用的问题

作者:梦在旅途 时间:2023-09-02 04:27:59 

我公司最近升级程序经常报出更新失败问题,究其原因,原来是更新时,他们可能又打开了正在被更新的文件,导致更新文件时,文件被其它进程占用,无法正常更新而报错,为了解决这个问题,我花了一周时间查询多方资料及研究,终于找到了一个查询进程的利器:handle.exe,下载地址:https://technet.microsoft.com/en-us/sysinternals/bb896655.aspx,我是通过它来找到被占用的进程,然后KILL掉占用进程,最后再来更新,这样就完美的解决了更新时文件被占用报错的问题了,实现方法很简单,我下面都有列出主要的方法,一些注意事项我也都有说明,大家一看就明白了,当然如果大家有更好的方案,欢迎交流,谢谢!

IsFileUsing:

判断文件是否被占用


[DllImport("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
 
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
 
public const int OF_READWRITE = 2;
public const int OF_SHARE_DENY_NONE = 0x40;
public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
private bool <strong>IsFileUsing</strong>(string filePath)
{
    if (!File.Exists(filePath))
    {
        return false;
    }
    IntPtr vHandle = _lopen(filePath, OF_READWRITE | OF_SHARE_DENY_NONE);
    if (vHandle == HFILE_ERROR)
    {
        return true;
    }
    CloseHandle(vHandle);
    return false;
}

GetRunProcessInfos:

获取指定文件或目录中存在的(关联的)运行进程信息,以便后面可以解除占用


/// <summary>
/// 获取指定文件或目录中存在的(关联的)运行进程信息,以便后面可以解除占用
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private Dictionary<int, string> GetRunProcessInfos(string filePath)
{
 
    Dictionary<int, string> runProcInfos = new Dictionary<int, string>();
    string fileName = Path.GetFileName(filePath);
    var fileRunProcs = Process.GetProcessesByName(fileName);
    if (fileRunProcs != null && fileRunProcs.Count() > 0)
    {
        runProcInfos = fileRunProcs.ToDictionary(p => p.Id, p => p.ProcessName);
        return runProcInfos;
    }
 
    string fileDirName = Path.GetDirectoryName(filePath); //查询指定路径下的运行的进程
    Process startProcess = new Process();
    startProcess.StartInfo.FileName = RelaseAndGetHandleExePath();
    startProcess.StartInfo.Arguments = string.Format("\"{0}\"", fileDirName);
    startProcess.StartInfo.UseShellExecute = false;
    startProcess.StartInfo.RedirectStandardInput = false;
    startProcess.StartInfo.RedirectStandardOutput = true;
    startProcess.StartInfo.CreateNoWindow = true;
    startProcess.StartInfo.StandardOutputEncoding = ASCIIEncoding.UTF8;
    startProcess.OutputDataReceived += (sender, e) =>
    {
        if (!string.IsNullOrEmpty(e.Data) && e.Data.IndexOf("pid:", StringComparison.OrdinalIgnoreCase) > 0)
        {
            //var regex = new System.Text.RegularExpressions.Regex(@"(^[\w\.\?\u4E00-\u9FA5]+)\s+pid:\s*(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            var regex = new System.Text.RegularExpressions.Regex(@"(^.+(?=pid:))\bpid:\s+(\d+)\s+", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            if (regex.IsMatch(e.Data))
            {
                var mathedResult = regex.Match(e.Data);
 
                int procId = int.Parse(mathedResult.Groups[2].Value);
                string procFileName = mathedResult.Groups[1].Value.Trim();
 
                if ("explorer.exe".Equals(procFileName, StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
 
                //var regex2 = new System.Text.RegularExpressions.Regex(string.Format(@"\b{0}.*$", fileDirName.Replace(@"\", @"\\").Replace("?",@"\?")), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                var regex2 = new System.Text.RegularExpressions.Regex(@"\b\w{1}:.+$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                string procFilePath = (regex2.Match(e.Data).Value ?? "").Trim();
 
                if (filePath.Equals(procFilePath, StringComparison.OrdinalIgnoreCase) || filePath.Equals(PathJoin(procFilePath, procFileName), StringComparison.OrdinalIgnoreCase))
                {
                    runProcInfos[procId] = procFileName;
                }
                else //如果乱码,则进行特殊的比对
                {
                    if (procFilePath.Contains("?") || procFileName.Contains("?")) //?乱码比对逻辑
                    {
                        var regex3 = new System.Text.RegularExpressions.Regex(procFilePath.Replace(@"\", @"\\").Replace(".", @"\.").Replace("?", ".{1}"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                        if (regex3.IsMatch(filePath))
                        {
                            runProcInfos[procId] = procFileName;
                        }
                        else
                        {
                            string tempProcFilePath = PathJoin(procFilePath, procFileName);
 
                            regex3 = new System.Text.RegularExpressions.Regex(tempProcFilePath.Replace(@"\", @"\\").Replace(".", @"\.").Replace("?", ".{1}"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                            if (regex3.IsMatch(filePath))
                            {
                                runProcInfos[procId] = procFileName;
                            }
                        }
                    }
                    else if (procFilePath.Length == filePath.Length || PathJoin(procFilePath, procFileName).Length == filePath.Length) //其它乱码比对逻辑,仅比对长度,如果相同交由用户判断
                    {
                        if (MessageBox.Show(string.Format("发现文件:{0}可能被一个进程({1})占用,\n您是否需要强制终止该进程?", filePath, procFileName), "发现疑似被占用进程", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            runProcInfos[procId] = procFileName;
                        }
                    }
                }
            }
        }
    };
 
    startProcess.Start();
    startProcess.BeginOutputReadLine();
    startProcess.WaitForExit();
 
    return runProcInfos;
}

上述代码逻辑简要说明:创建一个建程来启动handle.exe(以资源形式内嵌到项目中),然后异步接收返回数据,并通过正则表达式来匹配获取进程数据,由于handle.exe对于中文路径或文件名兼容不好,返回的数据存在?或其它乱码字符,故我作了一些特殊的模糊匹配逻辑;

RelaseAndGetHandleExePath:

从项目中释放handle.exe并保存到系统的APPData目录下,以便后续直接可以使用(注意:由于handle.exe需要授权同意后才能正常的使用该工具,故我在第一次生成handle.exe时,会直接运行进程,让用户选择Agree后再去进行后面的逻辑处理,这样虽能解决问题,但有点不太友好,目前一个是中文乱码、一个是必需同意才能使用handle.exe我认为如果微软解决了可能会更好)


private string RelaseAndGetHandleExePath()
{
    var handleInfo = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\SysUpdate\\handle.exe");
    if (!File.Exists(handleInfo.FullName))
    {
        if (!Directory.Exists(handleInfo.DirectoryName))
        {
            Directory.CreateDirectory(handleInfo.DirectoryName);
        }
 
        byte[] handleExeData = Properties.Resources.handle;
        File.WriteAllBytes(handleInfo.FullName, handleExeData);
 
        var handleProc = Process.Start(handleInfo.FullName);//若第一次,则弹出提示框,需要点击agree同意才行
        handleProc.WaitForExit();
    }
 
    return handleInfo.FullName;
}

PathJoin:

拼接路径(不过滤特殊字符),由于handle.exe对于中文路径或文件名兼容不好,返回的数据存在?或其它乱码字符,如查采用:Path.Combine方法则会报错,故这里自定义一个方法,只是简单的拼接


/// <summary>
/// 拼接路径(不过滤殊字符)
/// </summary>
/// <param name="paths"></param>
/// <returns></returns>
private string PathJoin(params string[] paths)
{
    if (paths == null || paths.Length <= 0)
    {
        return string.Empty;
    }
 
    string newPath = paths[0];
 
    for (int i = 1; i < paths.Length; i++)
    {
        if (!newPath.EndsWith("\\"))
        {
            newPath += "\\";
        }
 
        if (paths[i].StartsWith("\\"))
        {
            paths[i] = paths[i].Substring(1);
        }
 
        newPath += paths[i];
    }
 
    return newPath;
}

CloseProcessWithFile:

核心方法,关闭指定文件被占用的进程,上述所有的方法均是为了实现该方法的功能


private void CloseProcessWithFile(string filePath)
{
    if (!IsFileUsing(filePath)) return;
 
    ShowDownInfo(string.Format("正在尝试解除占用文件 {0}", _FilePaths[_FileIndex]));
 
    var runProcInfos = GetRunProcessInfos(filePath); //获取被占用的进程
 
 
    System.IO.File.WriteAllText(Path.Combine(Application.StartupPath, "runProcInfos.txt"), string.Join("\r\n", runProcInfos.Select(p => string.Format("ProdId:{0},ProcName:{1}", p.Key, p.Value)).ToArray()));//DEBUG用,正式发布时可以去掉
 
    var localProcesses = Process.GetProcesses();
    bool hasKilled = false;
    foreach (var item in runProcInfos)
    {
        if (item.Key != currentProcessId) //排除当前进程
        {
            var runProcess = localProcesses.SingleOrDefault(p => p.Id == item.Key);
            //var runProcess = Process.GetProcessById(item.Key);
            if (runProcess != null)
            {
                try
                {
                    runProcess.Kill(); //强制关闭被占用的进程
                    hasKilled = true;
                }
                catch
                { }
            }
        }
    }
 
    if (hasKilled)
    {
        Thread.Sleep(500);
    }
}

上述代码逻辑简要说明:先判断是否被占用,若被占用,则获取该文件被占用的进程列表,然后获取一下当前操作系统的所有进程列表,最后通过进程ID查询得到排除当前程序自己的进程ID(currentProcessId = Process.GetCurrentProcess().Id)列表,若能获取得到,表明进程仍在运行,则强制终止该进程,实现解除文件占用

注意:KILL掉占用进程后,可能由于缓存原因,若直接进行文件的覆盖与替换或转移操作,可能仍会报错,故这里作了一个判断,若有成功KILL掉进程,则需等待500MS再去做更新文件之类的操作;

来源:https://www.cnblogs.com/zuowj/p/5840567.html

标签:c#,handle.exe
0
投稿

猜你喜欢

  • java接口自动化测试框架及断言详解

    2022-01-23 20:01:36
  • MyBatis中XML 映射文件中常见的标签说明

    2023-01-07 08:02:59
  • 兼容Spring Boot 1.x和2.x配置类参数绑定的工具类SpringBootBindUtil

    2023-11-03 05:35:06
  • Java源码解析之ClassLoader

    2022-06-14 06:07:47
  • 关于C# dynamic装箱问题

    2021-09-13 19:22:40
  • Mybatis plus多租户方案的实战踩坑记录

    2023-08-01 05:19:09
  • 一款域名监控小工具 Domain(IP)Watcher 实现代码

    2023-09-15 11:55:44
  • 简单实现Android应用的启动页

    2022-04-15 14:23:45
  • SVN报错:Error Updating changes:svn:E155037的解决方案

    2023-06-11 07:27:11
  • Java利用Dijkstra算法求解拓扑关系最短路径

    2021-08-24 10:30:54
  • Android GPS定位测试(附效果图和示例)

    2023-05-11 17:38:10
  • android dialog自定义实例详解

    2022-03-15 20:29:22
  • C#对文件名智能排序的算法

    2022-05-02 16:18:28
  • mybatisPlus返回Map类型的集合

    2022-01-31 13:37:07
  • Java分层概念详解

    2021-12-12 06:29:18
  • 基于Beanutils.copyProperties()的用法及重写提高效率

    2023-04-20 12:10:25
  • C# 格式化JSON的两种实现方式

    2023-03-14 05:31:16
  • WPF中ImageBrush常用方式介绍

    2022-02-26 09:45:30
  • Android仿QQ微信侧滑删除效果

    2023-07-08 11:22:15
  • js中去除字符串中所有的html标签代码实例

    2023-03-24 16:23:36
  • asp之家 软件编程 m.aspxhome.com