1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | using System; using System.Diagnostics; using System.IO; using System.Text; namespace MD5Helper.Helper { public class MD5Helper { //定义一个用于保存静态变量的实例 private static MD5Helper instance = null ; //定义一个保证线程同步的标识 private static readonly object locker = new object (); //构造函数为私有,使外界不能创建该类的实例 private MD5Helper() { } public static MD5Helper Instance { get { if (instance == null ) { lock (locker) { if (instance == null ) instance = new MD5Helper(); } } return instance; } } /// <summary> /// 执行外部命令 /// </summary> /// <param name="cmd">命令参数</param> /// <param name="filePath">命令程序路径</param> /// <returns>执行结果</returns> public string ExecuteOutCmd( string cmd, string filePath) { using ( var process = new Process()) { ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.FileName = "cmd.exe" ; //starts cmd window startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.CreateNoWindow = true ; startInfo.RedirectStandardInput = true ; startInfo.RedirectStandardOutput = true ; startInfo.UseShellExecute = false ; //required to redirect process.StartInfo = startInfo; process.Start(); //string aa = "C:\\Windows\\System32\\cmd.exe /k cd /d " + '"' + applocaltion + '"'; //string bb = "certutil -hashfile " + "测试.pdf" + " MD5"; //SW.WriteLine("@echo on"); StreamWriter SW = process.StandardInput; SW.WriteLine( @"cd /d" + '"' + filePath + '"' ); //the command you wish to run..... SW.WriteLine(cmd); SW.WriteLine( "exit" ); //exits command prompt window StreamReader reader = process.StandardOutput; string str = reader.ReadToEnd(); process.WaitForExit(); process.Close(); return str; } } public string RunCmdBack( string cmd) { StringBuilder builder = new StringBuilder(); Process process = new Process(); try { process.StartInfo.FileName = "cmd.exe" ; //执行命令 process.StartInfo.Arguments = $ "/C {cmd}" ; process.StartInfo.UseShellExecute = false ; //不启用shell process.StartInfo.RedirectStandardInput = true ; process.StartInfo.RedirectStandardOutput = true ; process.StartInfo.RedirectStandardError = true ; process.StartInfo.CreateNoWindow = true ; //不使用窗口 process.Start(); StreamReader reader = process.StandardOutput; string line = "" ; //每次读取一行 while ((line = reader.ReadLine()) != null ) { builder.AppendLine(line); } process.WaitForExit(); process.Close(); reader.Close(); } catch (Exception e) { process.Close(); Console.WriteLine(e.Message); } return builder.ToString(); } } } |
RunCmdBack——入参:certutil -hashfile c:\3d4596eb-6f99-4e9f-bb68-a8672cb5dd46.pdf md5
返回值:(注意,文件夹路径中文件夹名称不能存在空格)
使用方式如下所示:
1 2 3 4 5 6 7 8 9 10 11 | private void btnGetHash_Click( object sender, EventArgs e) { string filePath = Application.StartupPath + "\\TestFile" ; string cmd = "certutil -hashfile " + "测试.pdf" + " MD5" ;<br> //filePath = "D:\\测试 空格 文件夹"; var cmdFileHash = MD5Helper.Helper.MD5Helper.Instance.ExecuteOutCmd(cmd, filePath); this .txtCMDHash.Text = cmdFileHash; string runcmd = "certutil -hashfile " + Application.StartupPath + "\\TestFile\\测试.pdf" + " MD5" ; this .txtCodeHash.Text = MD5Helper.Helper.MD5Helper.Instance.RunCmdBack(runcmd); } |
运行效果如下所示:
ExecuteOutCmd 测试文件夹有空格的文件夹名称,如下所示:
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本