C#操作cmd
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的更多相关文章
- C#中隐式操作CMD命令行窗口
原文:C#中隐式操作CMD命令行窗口 MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定 ...
- C# 直接使用sql语句对数据库操作 (cmd.ExecuteNonQuery)
只介绍读和删 不管使用什么方法来对数据库进行操作都绕不开和数据库的连接问题,所以咱们先在App.config中添加连接字段 <connectionStrings> <add name ...
- python模块——socket (实现简单的C/S架构端通信操作CMD)
# 服务端代码#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" import socket impo ...
- java操作CMD命令
import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; public class CM ...
- Windows CMD命令大全
http://greatverve.cnblogs.com/archive/2011/12/09/windows-cmd.html 命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机 ...
- Windows CMD命令大全【转】
命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一 ...
- Windows CMD命令大全(转)
Windows CMD命令大全 命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行 ...
- windows cmd 命令大全
原文: http://www.cnblogs.com/greatverve/archive/2011/12/09/windows-cmd.html 命令简介 cmd是command的缩写.即命令行 . ...
- CMD命令锦集
虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一般Windows 的各种版本都与其兼容,用户可以在Wi ...
随机推荐
- Python--数据类型整理
数据类型整理-------------------------------------------------------------------------------------------- ...
- Excel表格数据导入Mysql数据库的方法
1.使用Navicat 连接需要导入的数据库. 2.excel 列的名字最好和数据库的名字一致,便于我们直观的查看好理解. 第一步,先创建好表,和准备好对应的excel文件.在Navicat 中选 ...
- iOS用户是否打开APP通知开关跳转到系统的设置界面
1.检测用户是否打开推送通知 /** 系统通知是否打开 @return 是否打开 */ //检测通知是否打开iOS8以后有所变化 所以需要适配iOS7 + (BOOL)openThePushNoti ...
- 图像处理之滤波---滤波在游戏中的应用boxfilter
http://www.yxkfw.com/?p=7810 很有意思的全方位滤波应用 https://developer.nvidia.com/sites/default/files/akamai/ga ...
- android lanchmode
http://www.cnblogs.com/xiaoQLu/archive/2012/07/17/2595294.html http://www.cnblogs.com/lwbqqyumidi/p/ ...
- IOS8 TouchID使用介绍
本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35555123 IOS8将指纹识别技术开放出来了.我们能够利用用户设置的touch I ...
- SAM4E单片机之旅——7、LED闪烁之TC中断
RTT主要用做一个全局的定时器,而且不太通用.现在尝试使用一个更为通用的定时器进行定时:定时计数器(Timer Counter, TC). TC提供了广泛的功能,主要可以分为对输入的测量,以及波形的输 ...
- Android笔记之启动界面的设置
默认情况下,启动界面是白屏 我们自定义一个启动界面如下,3秒钟后进入主界面并结束启动页 SplashActivity.java package com.bu_ish.myapp; import and ...
- mysql 修改语法格式
1.修改字段注释格式 alter table {table} modify column {column} {type} comment '{comment}';
- 对于pod导入第三方库文件终端语言记录
//换成 pod install --verbose --no-repo-update //生成Podfile文件 touch Podfile 加上--verbose --no-repo-update ...