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

Cmd重定向的更多相关文章

  1. cmd 重定向

    关于cmd 命令的重定向输出 2>&1 mycommand >mylog.txt 2>&1 应该是最经典的用法了. 命令的结果可以通过" %> &qu ...

  2. <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序

    最近学习加密算法,需要生成素数表,一开始使用简单的循环,从2开始判断.代码如下: #include<iostream> #include<cstdio> #include< ...

  3. Windows 常用Cmd命令行 (持续更新...)

    查看IP ipconfig 查看WIFI密码 netsh wlan show profiles wifi_name key = clear 系统探针 systeminfo CMD重定向 输出符号> ...

  4. Kali Linux渗透基础知识整理(四):维持访问

    Kali Linux渗透基础知识整理系列文章回顾 维持访问 在获得了目标系统的访问权之后,攻击者需要进一步维持这一访问权限.使用木马程序.后门程序和rootkit来达到这一目的.维持访问是一种艺术形式 ...

  5. NetCat使用手册

    简介:   在网络工具中有“瑞士军刀”美誉的NetCat(以下简称nc),在我们用了N年了至今仍是爱不释手.因为它短小精悍(这个用在它身上很适合,现在有人已经将其修改成大约10K左右,而且功能不减少) ...

  6. IPC$命令详解

    一 摘要二 什么是ipc$三 什么是空会话四 空会话可以做什么五 ipc$所使用的端口六 ipc管道在hack攻击中的意义七 ipc$连接失败的常见原因八 复制文件失败的原因九 关于at命令和xp对i ...

  7. 445port入侵具体解释

    445port入侵具体解释   关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...

  8. 445port入侵详细解释

    445port入侵具体解释   关于"445port入侵"的内容445port入侵具体解释本站搜索很多其它关于"445port入侵"的内容 445port入侵, ...

  9. 空连接ipc$入侵

    使用命令 net use url=file://\\IP\ipc$\\IP\ipc$ "" /user:"" 就可以简单地和目标建立一个空连接(需要目标开放ip ...

随机推荐

  1. Pytorch中的自动求梯度机制和Variable类

    自动求导机制是每一个深度学习框架中重要的性质,免去了手动计算导数,下面用代码介绍并举例说明Pytorch的自动求导机制. 首先介绍Variable,Variable是对Tensor的一个封装,操作和T ...

  2. python常见数据类型及操作方法

    title: "python数据类型及其常用方法" date: 2020-04-21T10:15:44+08:00 可变数据类型:允许变量的值发生变化,即如果对变量进行append ...

  3. Python在Linux下编译安装

    [准备环境] Linux centos [前言] 1 linux下默认带Python,带的是2.7版本的 ,如果需要升级版本,需要把系统的自带的Python改名或者卸载,再次安装你所需要的Python ...

  4. 用 npm 搭建vue项目

    一.开发环境 vue推荐开发环境: Node.js: javascript运行环境(runtime),不同系统直接运行各种编程语言 npm: Nodejs下的包管理器. webpack: 它主要的用途 ...

  5. 【解读】TCP协议

    本文内容如下:      1)TCP协议概念      2)TCP头部结构和字段介绍      3)TCP流量控制            滑动窗口      4)TCP拥塞控制           慢 ...

  6. windows 64位上安装mysql 5.7版本

    下载的mysql不是安装exe的软件,而是在windows上编译好的二进制mysql软件 下载安装之后配置环境变量:将目录D:\Program Files\mysql-5.7.18-winx64\my ...

  7. Python3-随笔目录

      Python3-面向对象 标准库模块 Python3-collections模块-容器数据类型 Python3-datetime模块-日期与时间 Python3-re模块-正则表达式 Python ...

  8. Python 图像处理 OpenCV (12): Roberts 算子、 Prewitt 算子、 Sobel 算子和 Laplacian 算子边缘检测技术

    前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...

  9. openstack cinder-backup流程与源码分析

    在现在的云计算大数据环境下,备份容灾已经变成了一个炙手可热的话题,今天,和大家一起分享一下openstack是怎么做灾备的. [首先介绍快照] snapshot可以为volume创建快照,快照中保存了 ...

  10. SpringCloud之zuul