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;
}

C#操作cmd的更多相关文章

  1. C#中隐式操作CMD命令行窗口

    原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...

  2. C# 直接使用sql语句对数据库操作 (cmd.ExecuteNonQuery)

    只介绍读和删 不管使用什么方法来对数据库进行操作都绕不开和数据库的连接问题,所以咱们先在App.config中添加连接字段 <connectionStrings> <add name ...

  3. python模块——socket (实现简单的C/S架构端通信操作CMD)

    # 服务端代码#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" import socket impo ...

  4. java操作CMD命令

    import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; public class CM ...

  5. Windows CMD命令大全

    http://greatverve.cnblogs.com/archive/2011/12/09/windows-cmd.html 命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机 ...

  6. Windows CMD命令大全【转】

    命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一 ...

  7. Windows CMD命令大全(转)

    Windows CMD命令大全   命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行 ...

  8. windows cmd 命令大全

    原文: http://www.cnblogs.com/greatverve/archive/2011/12/09/windows-cmd.html 命令简介 cmd是command的缩写.即命令行 . ...

  9. CMD命令锦集

    虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一般Windows 的各种版本都与其兼容,用户可以在Wi ...

随机推荐

  1. 用callgraph生成的两张函数调用关系图

    参考这里,感觉很Cool吧. Linux-0.11函数调用关系图: QEMU函数调用关系图:

  2. angularJS 自定义指令 分页

    原理和使用说明 1.插件源码主要基于angular directive来实现. 2.调用时关键地方是后台请求处理函数,也就是从后台取数据. 3.插件有两个关键参数currentPage.itemsPe ...

  3. eclipse的快捷键(常用)

    1. Ctrl+O 显示类中方法和属性的大纲,能快速定位类的方法和属性,在查找Bug时非常有用. 2. Ctrl+M 窗口最大化和还原,用户在窗口中进行操作时,总会觉得当前窗口小(尤其在编写代码时), ...

  4. C#获取Access数据库中的所有表名和列名

    //C#获取Access数据库中的所有表名和列名    string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" ...

  5. centos编译 Compiling FFmpeg on CentOS RHEL Fedora

    This guide is based on a minimal installation of the latest CentOS release, and will provide a local ...

  6. 公网RTSP地址(持续更新)

    H264+AAC: rtsp://a2047.v1412b.c1412.g.vq.akamaistream.net/5/2047/1412/1_h264_350/1a1a1ae555c53196016 ...

  7. Conditions in bash scripting (if statements)

    Shell中判断语句if中-z至-d的意思 - sunny_2015 - 博客园 https://www.cnblogs.com/coffy/p/5748292.html Conditions in ...

  8. 20170313 ABAP程序未激活状态下保存或激活进入debug

    自动进入断点,没有设置的.FUNCTION RS_NEW_PROGRAM_INDEX. https://archive.sap.com/discussions/message/14132983 解决办 ...

  9. java URI 编码解码

    import java.io.UnsupportedEncodingException; /** * url转码.解码 */ public class UrlUtil { private final ...

  10. Windows Power Shell

    Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能. 它引入了许多非常有用的新概念,从而进一步扩展了您在 W ...