C# 通过命令行实现--文件 MD5返回值

欢迎莅临 SUN WU GANG 的园子!!! / 2023-05-19 / 原文

public  string runCmdBack(string cmd)
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                Process process = new Process();
                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)
                {
                    //Console.WriteLine(">>" + line);
                    builder.AppendLine(line);
                }

                process.WaitForExit();
                process.Close();
                reader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return builder.ToString();
        }

入参:certutil -hashfile c:\3d4596eb-6f99-4e9f-bb68-a8672cb5dd46.pdf  md5

返回值: