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
返回值:(注意,文件夹路径中文件夹名称不能存在空格)

使用方式如下所示:
private void btnGetHash_Click(object sender, EventArgs e)
{
string filePath = Application.StartupPath + "\\TestFile";
string cmd = "certutil -hashfile " + "测试.pdf" + " MD5";
//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 测试文件夹有空格的文件夹名称,如下所示:

博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!

浙公网安备 33010602011771号