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 ...
随机推荐
- Fedora 25/24/23 nVidia Drivers Install Guide
https://www.if-not-true-then-false.com/2015/fedora-nvidia-guide/ search Most Popular Featured Linux ...
- hdu1878欧拉回路(DFS+欧拉回路)
欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 使用sed来自动注释/恢复crontab中的一个任务
# 注释crontab任务crontab -l > ${WORK_DIR}/cron_binarysed -i 's%\(.*/home/xyz/xyz.sh\)%#\1%' ${WORK ...
- Python之Pandas库常用函数大全(含注释)
前言:本博文摘抄自中国慕课大学上的课程<Python数据分析与展示>,推荐刚入门的同学去学习,这是非常好的入门视频. 继续一个新的库,Pandas库.Pandas库围绕Series类型和D ...
- iOS 图像处理-剪裁图像
解决这个问题:依照某一长宽比例,剪裁图片的上部和下部.保留中间的内容.当然也能够自己定义须要剪裁留下的区域 前提:须要加入Framework:CoreGraphics.framework 代码: - ...
- Please read "Security" section of the manual to find out how to run mysqld as root!
[root@test ~]# /usr/local/mysql/bin/mysqld2018-08-05T08:29:05.143142Z 0 [Warning] [MY-011070] [Serve ...
- Snow White,摘自iOS应用Snow White and more stories
Once upon a time, there was a land. 从前,有个国度. It was ruled by an evil queen. 它被一位邪恶的女王统治. Every day t ...
- go 客户端服务端通信
client.go package main import ( "bufio" "encoding/json" "fmt" "ha ...
- Fastjson 简介
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. ...
- RabbitMQ的安装与客户端的简单实用
本文主要内容是RabbitMQ的安装步骤[Windows系统与linux上的安装]及客户端的简单使用. 1.下载 下载地址:http://www.rabbitmq.com/download.html ...