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命令的更多相关文章

  1. JAVA之执行cmd命令

    感言在前:时隔好久没有更新博客园了,忙东忙西也没忙出个什么之所以然来.回首过去一两个月,只能用“疲倦”两个字来形容,而且是身心疲惫.每天11.12个小时的工作我都没觉得烦,但是总是想克服却又很难克服的 ...

  2. [转]Delphi执行CMD命令

    今天看到有人在问用代码执行CMD命令的问题,就总结一下用法,也算做个备忘. Delphi中,执行命令或者运行一个程序有2个函数,一个是winexec,一个是shellexecute.这两个大家应该都见 ...

  3. C# 执行CMD 命令

    /// <summary> /// 执行CMD 命令 /// </summary> /// <param name="strCommand">命 ...

  4. C# 执行CMD命令的方法

    /// <summary> /// 执行CMD命令 /// </summary> /// <param name="str"></para ...

  5. 如何使用Java执行cmd命令

    用JAVA代码实现执行CMD命令的方法! Runtime rt = Runtime.getRuntime(); Process p = rt.exec(String[] cmdarray);     ...

  6. java执行cmd命令并获取输出结果

    1.java执行cmd命令并获取输出结果 import java.io.BufferedReader; import java.io.InputStreamReader; import org.apa ...

  7. 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 ...

  8. Atitit.执行cmd 命令行 php

    Atitit.执行cmd 命令行 php 1. 执行cmd 命令行,调用系统命令的基础 1 1.1. 实际执行模式 1 1.2. 空格的问题 1 1.3. 中文路径的问题,程序文件读取编码设置 1 1 ...

  9. node执行cmd命令方法

    var cmd='tasklist';//获取 子进程模块的exec方法,用于执行cmd命令var exec = require('child_process').exec; //运行 定义的cmd命 ...

随机推荐

  1. hihoCoder#1062 最近公共祖先·

    原题地址 A和A的共同祖先是A,即使A没有在之前的家谱中出现过!被这个坑了,WA了很久... 比如:小头爸爸是大头儿子他爹,问:隔壁王叔叔和隔壁王叔叔的最近祖先是谁?,答:隔壁王叔叔. 代码: #in ...

  2. noip模拟赛 道路分组

    分析:因为每一组编号都是连续的嘛,所以能分成一组的尽量分,每次加边后dfs判断一下1和n是否连通.有向图的判连通没有什么很快的方法,特别注意,并查集是错的!这个算法可以得到60分. 事实上每一次都不需 ...

  3. Codeforces700C. Break Up

    n<=1000,m<=30000的图,问割掉边权和尽量小的0.1或2条边使S和T不连通,输出割了哪些边,无解-1. 道理是很好懂的,先随便找S到T的一条路径,找不到输出0,找到的话这条路上 ...

  4. Thinkphp5.0 的使用模型Model的获取器与修改器

    Thinkphp5.0 的使用模型Model的获取器.修改器.软删除 一.获取器 在model中使用 get+字段名+Attr,可以修改字段的返回值. 数据库中性别保存为,0未知.1男.2女,查询时返 ...

  5. TOMCAT加载两次war包(重复加载)

    一.问题描述 项目中通过配置Context节点docBase,使docBase指向项目的绝对路径,可以直接通过IP加端口访问,今日发现意外bug,项目中某个功能奇数次执行成功,偶数次执行失败.二.问题 ...

  6. [bzoj1978][BeiJing2010]取数游戏 game_动态规划_质因数分解

    取数游戏 game bzoj-1978 BeiJing-2010 题目大意:给定一个$n$个数的$a$序列,要求取出$k$个数.假设目前取出的数是$a_j$,那么下次取出的$a_k$必须保证:$j&l ...

  7. codevs——1081 线段树练习 2

    1081 线段树练习 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 给你N个数,有两种操作 1:给 ...

  8. SqlServer函数获取指定日期后的第某个工作日

    获取工作日 需要编写一个SqlServer函数,F_getWorkday,传入两个参数,第一个为时间date,第二个参数为第几个工作日num.调用F_getWorkday后返回date之后的第num个 ...

  9. MapReduce WordCount Combiner程序

    MapReduce WordCount Combiner程序 注意使用Combiner之后的累加情况是不同的: pom.xml <project xmlns="http://maven ...

  10. NA路由②

     CISCO常见的命令语法:     R(c)#ip route network {mask} address/interface :                     参数   {}可选项   ...