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. CDN百科第四讲 | 如何优雅地在云上“摆摊”——做直播带货,你不得不关注的技术

    最近,国家政策开始鼓励“地摊经济”,一时间各家企业平台纷纷推出地摊扶持政策,地摊概念股顺势大涨,地摊生态及配套商品也开始走俏,甚至在网络上也涌现出各种“新摊主速成攻略”,万亿的烟火经济俨然已经走上风口 ...

  2. c常用函数-sprintf

    sprintf sprinti函数的作用是把一个字符串格式化后输入到另一个字符串中,然后返回写入的·字符数量. Sprinf在用法上和1.2.3节的prinf函数一致,区别是sprintf输出结果到指 ...

  3. ESP8266服务器模式 发送数据和接收数据 模板1

    功能如下: 1.将客户端发来的数据转发到串口:2.串口数据转发给所有客户端3.可连接4个客户端4.可设置静态IP地址5.指示灯闪烁表示无客户端连接,灯亮代表有客户端连接 /** 功能: 1.将客户端发 ...

  4. vc6.0创建文件时,出现很多烫烫烫解决方法

    vc6.0创建文件时,出现很多烫烫烫烫解决方法 SWM2烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫.IA 如果项目文件放在桌面上 如:C:\Documents and Settings\txwtec ...

  5. WeChair项目Alpha冲刺(8/10)

    团队项目进行情况 1.昨日进展    Alpha冲刺第八天 昨日进展: 前端:安排页面美化,设计实名认证 后端:用户信息通过dao层存储数据库 数据库:修改数据表属性,与后端部署数据库交互 2.今日安 ...

  6. java创建图片的缩略图

    //java创建图片的缩略图private void createThumbnail(String filename, int thumbWidth, int thumbHeight, int qua ...

  7. 第三模块 :微服务网关Zuul架构和实践

    52.Zuul网关架构剖析~1.mp4 请求过来之后首先会通过前置过滤器,然后到路由过滤器,路由过滤器是真正访问后台远程服务的,经过路由器之后,最后会传递给后置过滤器 在三个过滤器处理的过程中任何一个 ...

  8. 黎活明8天快速掌握android视频教程--21_监听ContentProvider中数据的变化

    采用ContentProvider除了可以让其他应用访问当前的app的数据之外,还有可以实现当app的数据发送变化的时候,通知注册了数据变化通知的调用者 其他所有的代码都和第20讲的一样,不同的地方看 ...

  9. android 事件分发机制2-案例测试

    我们来看程序的代码: 要求: 1.通过手指移动来拖动图片 2.控制图片不能超出屏幕显示区域 技术点: 1.MotionEvent处理 2.对View进行动态定位(layout) package im. ...

  10. django 分页器,序列化 ,MTV MVC

    序列化组件## from django.core import serializers # django自带的一个小型的序列化工具# def reg(request):# user_list = mo ...