1、执行单条cmd命令

public static string ExecuteCmd(string command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
//p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
p.Start();//启动程序
//消除三行启动信息
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
//向cmd窗口发送输入信息
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
string error = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
p.WaitForExit();
p.Close();
return result;
}

2、一次执行多条cmd命令

public static string ExecuteCmd(string[] commands)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
//p.StandardInput.AutoFlush = true;//每次写入命令后刷新,报未StandardInput未重定向
//p.StartInfo.Arguments = "/c " + command;///c代表执行命令后关闭cmd.exe /k参数则不关闭
p.Start();//启动程序
//消除三行启动信息
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine();
p.StandardOutput.ReadLine(); //向cmd窗口发送输入信息
foreach (string cmd in commands)
{
p.StandardInput.WriteLine(cmd);
p.StandardInput.Flush();
}
p.StandardInput.WriteLine("exit");
string result = p.StandardOutput.ReadToEnd().Replace(Environment.CurrentDirectory, "");
string error = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
p.WaitForExit();
p.Close(); return result;
}

3、利用cmd重定向注册DLL、创建服务、加载证书

public static string RegsvrFile(string filePath, bool reg = true, bool fileBit64 = false)
{
string result;
string[] commands = { "", "" };
if (!fileBit64 && Environment.Is64BitOperatingSystem)
commands[0] = "cd /d %WinDir%\\SysWOW64";
if (reg)
commands[1] = "regsvr32 /s \"" + filePath + "\"";
else
commands[1] = "regsvr32 /u /s \"" + filePath + "\"";
if (string.IsNullOrWhiteSpace(commands[0]))
result = CmdOper.ExecuteCmd(commands[1]);
else
result = CmdOper.ExecuteCmd(commands);
return result;
} public static string ScCreate(string filePath, string serviceName, string displayName, string description, string start="auto", string depend=null)
{
string result;
string[] commands = { "", "" };
if (string.IsNullOrWhiteSpace(depend))
{
commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\"",
serviceName, filePath, start, displayName);
}
else
{
commands[0] = string.Format("sc create {0} binPath= \"{1}\" type= share start= {2} error= ignore DisplayName= \"{3}\" depend= {4}",
serviceName, filePath, start, displayName, depend);
}
if (!string.IsNullOrWhiteSpace(description))
{
commands[1] = string.Format("sc description {0} \"{1}\"", serviceName, description);
}
if (string.IsNullOrWhiteSpace(commands[1]))
result = CmdOper.ExecuteCmd(commands[0]);
else
result = CmdOper.ExecuteCmd(commands); return result;
} public static string ScStart(string serviceName)
{
string command = "net start " + serviceName;
return CmdOper.ExecuteCmd(command);
} public static string ScStop(string serviceName)
{
string command = "net stop " + serviceName;
return CmdOper.ExecuteCmd(command);
} public static string ScDelete(string serviceName)
{
string[] commands = { "net stop " + serviceName, "sc delete " + serviceName };
return CmdOper.ExecuteCmd(commands);
} public static string CertFile(string filePath, bool install=true)
{
string result;
string[] commands = { "", "" };
if (Environment.Is64BitOperatingSystem)
commands[0] = "cd /d %WinDir%\\SysWOW64";
if (install)
commands[1] = "certutil -addstore root \"" + filePath + "\"";
else
commands[1] = "certutil -delstore root \"" + filePath + "\"";
if (string.IsNullOrWhiteSpace(commands[0]))
result = CmdOper.ExecuteCmd(commands[1]);
else
result = CmdOper.ExecuteCmd(commands);
return result;
}

最新文章

  1. java使double保留两位小数的多方法 java保留两位小数
  2. 16s及宏基因组测序公司资源--20161104
  3. MySQL中删除重复数据的简单方法,mysql删除重复数据
  4. cocos2d-x游戏循环与调度
  5. Apache 使用密码文件验证用户
  6. cocos2d_随手篇1_关于ccTouchBegan的调用
  7. spark shuffle
  8. PyQt5目录
  9. Ubuntu14.04安装nvidia-docker2
  10. Windows 下安装 Ubuntu 双系统(更新)
  11. 并发,同步锁,Runnable,Callable,Future
  12. Java的组合排列问题
  13. 如何取消浏览器护眼色 Lodop打印图片有窗口颜色的边框
  14. hdu 4685(强连通分量+二分图的完美匹配)
  15. 转,敏感词过滤,PHP实现的Trie树
  16. 大数据系列之并行计算引擎Spark介绍
  17. NameNode的ZKFC机制
  18. 简单原始的ASP.NET WEBFORM中多文件上传【参考其他资料修改】
  19. centos7用xshell可以连接, xftp连接失败!(墙裂推荐)
  20. 【驱动】网卡驱动&#183;linux内核网络分层结构

热门文章

  1. fastjson对String、JSONObject、JSONArray相互转换
  2. Java中的map集合顺序如何与添加顺序一样
  3. rest_framework django 简单使用(数据库创建数据, 覆盖数据, 其他的大同小异)
  4. 全网最全fiddler使用教程和fiddler如何抓包(fiddler手机抓包)-笔者亲测
  5. Linux下安装MongoDB 4.2数据库--使用tar包方式
  6. TCP端口扫描类型-隐蔽扫描和僵尸扫描
  7. SQL注入入门
  8. Java常用的文档地址
  9. VC单选按钮控件(Radio Button)用法(转)
  10. onunload对应的js代码为什么不能执行?和onbeforeunload的区别?