Winform执行CMD命令
1、首先分享CmdHelper类:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics; namespace Helper
{
/// <summary>
/// 执行命令
/// </summary>
public class CmdHelper
{
///
/// 执行cmd.exe命令
///
///命令文本
/// 命令输出文本
public static string ExeCommand(string commandText)
{
return ExeCommand(new string[] { commandText });
}
///
/// 执行多条cmd.exe命令
///
///命令文本数组
/// 命令输出文本
public static string ExeCommand(string[] commandTexts)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
foreach (string item in commandTexts)
{
p.StandardInput.WriteLine(item);
}
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
//strOutput = Encoding.UTF8.GetString(Encoding.Default.GetBytes(strOutput));
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
///
/// 启动外部Windows应用程序,隐藏程序界面
///
///应用程序路径名称
/// true表示成功,false表示失败
public static bool StartApp(string appName)
{
return StartApp(appName, ProcessWindowStyle.Hidden);
}
///
/// 启动外部应用程序
///
///应用程序路径名称
///进程窗口模式
/// true表示成功,false表示失败
public static bool StartApp(string appName, ProcessWindowStyle style)
{
return StartApp(appName, null, style);
}
///
/// 启动外部应用程序,隐藏程序界面
///
///应用程序路径名称
///启动参数
/// true表示成功,false表示失败
public static bool StartApp(string appName, string arguments)
{
return StartApp(appName, arguments, ProcessWindowStyle.Hidden);
}
///
/// 启动外部应用程序
///
///应用程序路径名称
///启动参数
///进程窗口模式
/// true表示成功,false表示失败
public static bool StartApp(string appName, string arguments, ProcessWindowStyle style)
{
bool blnRst = false;
Process p = new Process();
p.StartInfo.FileName = appName;//exe,bat and so on
p.StartInfo.WindowStyle = style;
p.StartInfo.Arguments = arguments;
try
{
p.Start();
p.WaitForExit();
p.Close();
blnRst = true;
}
catch
{
}
return blnRst;
} /// <summary>
/// 实现压缩,需要rar.exe上传到网站根目录
/// </summary>
/// <param name="s"></param>
/// <param name="d"></param>
/// <example>rar("e:/www.svnhost.cn/", "e:/www.svnhost.cn.rar");</example>
public static void Rar(string s, string d)
{
ExeCommand(System.Web.HttpContext.Current.Server.MapPath("~/rar.exe") + " a \"" + d + "\" \"" + s + "\" -ep1");
} /// <summary>
/// 实现解压缩,需要rar.exe上传到网站根目录
/// </summary>
/// <param name="s"></param>
/// <param name="d"></param>
/// <example>unrar("e:/www.svnhost.cn.rar", "e:/");</example>
public static void UnRar(string s, string d)
{
ExeCommand(System.Web.HttpContext.Current.Server.MapPath("~/rar.exe") + " x \"" + s + "\" \"" + d + "\" -o+");
} } }
2、利用CmdHelper类执行操作:
(1)检查Winddows激活有效期:
CmdHelper.ExeCommand("slmgr.vbs -xpr");
(2)计算器
CmdHelper.ExeCommand("calc");
(3)记事本
CmdHelper.ExeCommand("notepad");
(4)注册表编辑器
CmdHelper.ExeCommand("regedit");
(5)计算机性能监测
CmdHelper.ExeCommand("perfmon.msc");
(6)任务管理器
CmdHelper.ExeCommand("taskmgr");
(7)系统版本
CmdHelper.ExeCommand("winver");
(8)画图板
CmdHelper.ExeCommand("mspaint");
(9)远程桌面连接
CmdHelper.ExeCommand("mstsc");
(10)放大镜
CmdHelper.ExeCommand("magnify");
(11)DirectX诊断工具
CmdHelper.ExeCommand("dxdiag");
(12)屏幕键盘
CmdHelper.ExeCommand("osk");
(13)事件查看器
CmdHelper.ExeCommand("eventvwr");
(14)造字程序
CmdHelper.ExeCommand("eudcedit");
(15)字符映射表
CmdHelper.ExeCommand("charmap");
(16)注销
CmdHelper.ExeCommand("logoff");
(17)关机
CmdHelper.ExeCommand("shutdown -s -t 00");
(18)60s后关机
CmdHelper.ExeCommand("shutdown -s -t 60");
(19)重启
CmdHelper.ExeCommand("shutdown -r -t 00");
(20)取消关机/重启指令
CmdHelper.ExeCommand("shutdown -a");
Winform执行CMD命令的更多相关文章
- JAVA之执行cmd命令
感言在前:时隔好久没有更新博客园了,忙东忙西也没忙出个什么之所以然来.回首过去一两个月,只能用“疲倦”两个字来形容,而且是身心疲惫.每天11.12个小时的工作我都没觉得烦,但是总是想克服却又很难克服的 ...
- [转]Delphi执行CMD命令
今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...
- C# 执行CMD 命令
/// <summary> /// 执行CMD 命令 /// </summary> /// <param name="strCommand">命 ...
- C# 执行CMD命令的方法
/// <summary> /// 执行CMD命令 /// </summary> /// <param name="str"></para ...
- 如何使用Java执行cmd命令
用JAVA代码实现执行CMD命令的方法! Runtime rt = Runtime.getRuntime(); Process p = rt.exec(String[] cmdarray); ...
- java执行cmd命令并获取输出结果
1.java执行cmd命令并获取输出结果 import java.io.BufferedReader; import java.io.InputStreamReader; import org.apa ...
- Java 调用并执行cmd命令
cmd java 调用 执行 概要: Java 调用并执行cmd命令 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...
- Atitit.执行cmd 命令行 php
Atitit.执行cmd 命令行 php 1. 执行cmd 命令行,调用系统命令的基础 1 1.1. 实际执行模式 1 1.2. 空格的问题 1 1.3. 中文路径的问题,程序文件读取编码设置 1 1 ...
- node执行cmd命令方法
var cmd='tasklist';//获取 子进程模块的exec方法,用于执行cmd命令var exec = require('child_process').exec; //运行 定义的cmd命 ...
随机推荐
- 【扫描线】Educational Codeforces Round 4 D. The Union of k-Segments
http://codeforces.com/contest/612/problem/D [题解] http://blog.csdn.net/strokess/article/details/52248 ...
- hdu 4431 绝对值之和最小公式
/* 普通的二分不好写,反正我没写出来,这题核心需要求出绝对值最小公式 sum=|x+10|+|x+5|+|x+1|+|x-2|+|x-6|;sumx[1]=-10;sumx[2]=-15;sumx[ ...
- 【ZJOI2017 Round1后记】
2017.4.1: NOIP+Round1综合成绩出来,标准分离续命线差了80分,果然还是联赛坑挖太大了…… 不管怎么说能续命的话还是要试一下的…… 发毒誓:Round2前不打手游,不看NGA,不看星 ...
- Codeforces700C. Break Up
n<=1000,m<=30000的图,问割掉边权和尽量小的0.1或2条边使S和T不连通,输出割了哪些边,无解-1. 道理是很好懂的,先随便找S到T的一条路径,找不到输出0,找到的话这条路上 ...
- 解决json_encode中文乱码
在使用json_encode之前把字符用函数urlencode()处理一下,然后再json_encode,输出结果的时候在用函数urldecode()转回来
- 在mysql数据库中,文章表设计有啥好的思路
Q: 用mysql设计一张文章表,不知道有啥好的思路! 我是这样的,应为考虑附件和图片,所以我的文章表除了有varchar(1000)的文章内容,还设置了个Bolb接收附件和图片. 我用的是mysql ...
- Ubuntu 16.04安装Redis
版本:4.0.2 下载地址:https://redis.io/download 离线版本:(链接: https://pan.baidu.com/s/1bpwDtOr 密码: 4cxk) 安装过程: 源 ...
- 条款八: 写operator new和operator delete时要遵循常规
自己重写operator new时(条款10解释了为什么有时要重写它),很重要的一点是函数提供的行为要和系统缺省的operator new一致.实际做起来也就是:要有正确的返回值:可用内存不够时要调用 ...
- 【python】一些好的学习网址
http://www.cnblogs.com/BeginMan/p/3179302.html http://www.cnblogs.com/huxi/category/251137.html http ...
- Dell PowerEdge RAID Controller (PERC) | Dell
Dell PowerEdge RAID Controller (PERC) The Dell™ PERC (PowerEdge™ RAID Controller) fami ...