C#经常操作CMD,使用的话就用下面的2和3进行整理一下使用吧。

1、简单的调用命令不需要回传数据,最简单

public static void ipcmd(object p)
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + p;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
startInfo.Verb = "RunAs";
process.Start();
}

2、有回传数据的

public static string CmdExcute(string cmdStr)
{
Process process = new Process();
string output = ""; IntPtr ptr = new IntPtr();
bool bOS_X64 = System.Environment.Is64BitOperatingSystem;
if (bOS_X64)
{
Win32.Wow64DisableWow64FsRedirection(ref ptr); // 关闭system32文件重定向
} try
{
process.StartInfo.FileName = @"cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true; if (process.Start())//开始进程
{
process.StandardInput.AutoFlush = true;
process.StandardInput.WriteLine(cmdStr);
process.StandardInput.WriteLine("exit");
output = process.StandardOutput.ReadToEnd();
}
}
catch (Exception e)
{
}
finally
{
if (process != null)
{
process.Close();
}
}
if (bOS_X64)
{
Win32.Wow64RevertWow64FsRedirection(ptr); //恢复文件重定向
}
return output;
}
}

3、截取输出流的

public static List<string> cmd(string p)
{
List<string> lines = new List<string>();
List<string> lineNet = new List<string>();
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + p;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
startInfo.Verb = "RunAs"; try
{
process.Start();
System.IO.StreamReader reader = process.StandardOutput;//截取输出流 while (!reader.EndOfStream)
{
//if (reader.ReadLine().ToString().Contains("已禁用") || reader.ReadLine().ToString().Contains("已启用") || reader.ReadLine().ToString().Contains("Enabled") || reader.ReadLine().ToString().Contains("Disabled"))
//{
lineNet.Add(reader.ReadLine());
//}
}
foreach (var item in lineNet)
{
if (item.Contains("已禁用") || item.Contains("已启用") || item.Contains("Disabled") || item.Contains("Enabled"))
{
lines.Add(item);
}
}
}
catch (Exception e)
{ }
finally
{
if (process != null)
{
process.Close();
}
}
return lines;
}

最新文章

  1. ssh 登录慢?
  2. 三天学会HTML5 之第一天
  3. 点餐APP 冲刺三总结
  4. Centos7最小化安装后(minimal)安装图形界面
  5. ubuntu lua安装
  6. &lt;1&gt;数据引用与匿名存储
  7. 【HDOJ】3436 Queue-jumpers
  8. vbs的一些入门基础。。。
  9. HDU--2021
  10. CC2650LaunchPad 运行contiki hello-world示例程序
  11. C#-----创建DataTable对象
  12. 微服务之springCloud-docker-feign-hystrix-ribbon(七)
  13. Android-Activity跳转时动画
  14. 180727-时序数据库InfluxDB之备份和恢复策略
  15. Delphi使程序的窗口出现在最前面并激活
  16. 开源用户界面和布局的套件XiaoCai.WinformUI(美化用户界面利器)
  17. iOS:MBProgressHUD的基本使用
  18. LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)
  19. C# 读xml注释或过滤xml注释
  20. Maven修改默认本地资源库文件夹

热门文章

  1. C#Soket组播
  2. Android-可随意拖动的View
  3. MVC入门——编辑页
  4. LeetCode(88)题解-- Merge Sorted Array
  5. XML解析PULL
  6. 话题讨论&amp;amp;征文--谈论大数据时我们在谈什么 获奖名单发布
  7. 开源流媒体播放器EasyPlayer
  8. ABAP 动态内标排序
  9. Hibernate总结(转)
  10. Vue一次性简洁明了引入所有公共组件