• C#中隐式操作CMD命令行窗口 (转)


    MS的CMD命令行是一种重要的操作界面,一些在C#中不那么方便完成的功能,在CMD中几个简单的命令或许就可以轻松搞定,如果能在C#中能完成CMD窗口的功能,那一定可以使我们的程序简便不少。

    下面介绍一种常用的在C#程序中调用CMD.exe程序,并且不显示命令行窗口界面,来完成CMD中各种功能的简单方法。

    如下所示:

    System.Diagnosties.Process p=new
    System.Diagnosties.Process();
    p.StartInfo.FileName="cmd.exe";//要执行的程序名称
    p.StartInfo.UseShellExecute=false;
    p.StartInfo.RedirectStanderInput=true;//可能接受来自调用程序的输入信息
    p.StartInfo.RedirectStanderOutput=true;//由调用程序获取输出信息
    p.StartInfo.CreateNoWindow=true;//不显示程序窗口
    p.Start();//启动程序
    //向CMD窗口发送输入信息:
    p.StanderInput.WriteLine("shutdown
    -r t 10"); //10秒后重启(C#中可不好做哦)
    //获取CMD窗口的输出信息:
    string sOutput =
    p.StandardOutput.ReadToEnd();有啦以下代码,就可以神不知鬼不觉的操作CMD啦。总之,Process类是一个非常有用的类,它十分方便的利用第三方的程序扩展了C#的功能。

    最好的实现方式是使用事件:

    // some of the flags are not needed 
        process.StartInfo.CreateNoWindow=true; 
        process.StartInfo.ErrorDialog=false; 
        process.StartInfo.UseShellExecute=false; 
        process.StartInfo.RedirectStandardError=true; 
        process.StartInfo.RedirectStandardInput=true; 
        process.StartInfo.RedirectStandardOutput=true; 
        process.EnableRaisingEvents=true; 
        process.OutputDataReceived+= process_OutputDataReceived; 
        process.ErrorDataReceived+= process_OutputDataReceived; 
        process.Exited+= process_Exited; 
        process.Start(); 
        process.BeginErrorReadLine(); 
        process.BeginOutputReadLine();void process_Exited(object sender,System.EventArgs e){// do something when process terminates;}void process_OutputDataReceived(object sender,DataReceivedEventArgs e){// a line is writen to the out stream. you can use it like:string s = e.Data;}void process_ErrorDataReceived(object sender,DataReceivedEventArgs e){// a line is writen to the out stream. you can use it like:string s = e.Data;}
    

    c#执行shell

    private void Form1_Load(object sender, EventArgs e)

    {

    //实例一个process类

    Process process = new Process();

    //设定程序名

    process.StartInfo.FileName = "cmd.exe";

    //关闭Shell的使用

    process.StartInfo.UseShellExecute = false;

    //重新定向标准输入,输入,错误输出

    process.StartInfo.RedirectStandardInput = true;

    process.StartInfo.RedirectStandardOutput = true;

    process.StartInfo.RedirectStandardError = true;

    //设置cmd窗口不显示

    process.StartInfo.CreateNoWindow = true;

    //开始

    process.Start();

    //输入命令,退出

    process.StandardInput.WriteLine("ping 192.168.0.1");

    //process.StandardInput.WriteLine("netstat");

    process.StandardInput.WriteLine("exit");

    //获取结果

    string strRst = process.StandardOutput.ReadToEnd();

    //显示结果到RichTextBox

    this.richTextBox1.Text = strRst;

    }

    线程重定向问题

    描叙:打开cmd窗口,进入要编译的目录的d:linuxcase_3,在cmd窗口打开grads程序,在grads程序中执行out.gs,退出grads,退出cmd
    编译错误:未重定向
    private void Form1_Load(object sender, EventArgs e)
    {

    //实例一个process类
    Process process = new Process();
    //设定程序名
    process.StartInfo.FileName = "cmd.exe";
    //关闭Shell的使用
    process.StartInfo.UseShellExecute = false;
    //重新定向标准输入,输入,错误输出
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    //设置cmd窗口不显示
    process.StartInfo.CreateNoWindow = false;
    //开始
    process.Start();
    //输入命令,退出
    process.StandardInput.WriteLine(@"cd d:linuxcase_3");
    //在cmd中打开grads程序,在进程中产生grads.exe
    process.StandardInput.WriteLine("grads");
    process.StandardInput.WriteLine("y");


    bool isIniit = true;
    Process processTemp;
    while (isIniit)
    {
    Process[] processeszu = System.Diagnostics.Process.GetProcesses();//获取系统进程

    string processnamelist = string.Empty;
    for (int i = 0; i < processeszu.Length - 1; i++)
    {
    processTemp = processeszu;
    if (processTemp.ProcessName == "Grads")//判断Grads这个进程是否打开
    {
    processTemp.StartInfo.RedirectStandardInput = true;
    processTemp.StandardInput.WriteLine(@"out.gs");
    processTemp.StandardInput.WriteLine("quit");
    isIniit = false;
    break;
    }

    }
    }


    process.StandardInput.WriteLine("exit");

    //获取结果

    //显示结果到RichTextBox
    this.richTextBox1.Text = process.StandardOutput.ReadToEnd();
    }

    Process类详细介绍

    //启动浏览器,并打开指定的地址
    C#中隐式操作CMD命令行窗口
    string ipAddress = "127.0.0.1";
    C#中隐式操作CMD命令行窗口
    try
    C#中隐式操作CMD命令行窗口
    {
    C#中隐式操作CMD命令行窗口 IPHostEntry ipHost
    = Dns.Resolve(Dns.GetHostName());
    C#中隐式操作CMD命令行窗口 ipAddress
    = ipHost.AddressList[0].ToString().Trim();
    C#中隐式操作CMD命令行窗口 }

    C#中隐式操作CMD命令行窗口
    catch (Exception)
    C#中隐式操作CMD命令行窗口
    {
    C#中隐式操作CMD命令行窗口 }

    C#中隐式操作CMD命令行窗口
    C#中隐式操作CMD命令行窗口
    C#中隐式操作CMD命令行窗口 Process p
    = new Process();
    C#中隐式操作CMD命令行窗口 p.StartInfo.FileName
    = "iexplore.exe";
    C#中隐式操作CMD命令行窗口 p.StartInfo.Arguments
    = "http://" + ipAddress + "/WebFinanceAllowance/";
    C#中隐式操作CMD命令行窗口 p.Start();
    C#中隐式操作CMD命令行窗口 p.Close();
    C#中隐式操作CMD命令行窗口
    C#中隐式操作CMD命令行窗口
    C#中隐式操作CMD命令行窗口
    //关闭打开的进程
    C#中隐式操作CMD命令行窗口
    if (!p.HasExited)
    C#中隐式操作CMD命令行窗口
    {
    C#中隐式操作CMD命令行窗口 p.CloseMainWindow();
    C#中隐式操作CMD命令行窗口 }

    1C#中隐式操作CMD命令行窗口//启动资源管理器,并打开指定路径
    2C#中隐式操作CMD命令行窗口 Process.Start("explorer.exe", "C:\mysql");
    C#中隐式操作CMD命令行窗口//使用process类来执行dos命令
    C#中隐式操作CMD命令行窗口
    Process process = new Process();
    C#中隐式操作CMD命令行窗口
    process.StartInfo.FileName
    = "cmd.exe"
    ;
    C#中隐式操作CMD命令行窗口
    process.StartInfo.UseShellExecute
    = false
    ;
    C#中隐式操作CMD命令行窗口
    process.StartInfo.RedirectStandardInput
    = true
    ;
    C#中隐式操作CMD命令行窗口
    process.StartInfo.RedirectStandardOutput
    = true
    ;
    C#中隐式操作CMD命令行窗口
    process.StartInfo.RedirectStandardError
    = true
    ;
    C#中隐式操作CMD命令行窗口
    process.StartInfo.CreateNoWindow
    = true
    ;
    C#中隐式操作CMD命令行窗口
    C#中隐式操作CMD命令行窗口

    C#中隐式操作CMD命令行窗口
    C#中隐式操作CMD命令行窗口
    //开始卸载

    C#中隐式操作CMD命令行窗口
    process.Start();
    C#中隐式操作CMD命令行窗口
    process.StandardInput.WriteLine(
    "cd C:\mysql\bin"
    );
    C#中隐式操作CMD命令行窗口
    process.StandardInput.WriteLine(
    "net stop mysql"
    );
    C#中隐式操作CMD命令行窗口
    process.StandardInput.WriteLine(
    "mysqld -remove"
    );
    C#中隐式操作CMD命令行窗口
    process.StandardInput.WriteLine(
    "exit"
    );
    C#中隐式操作CMD命令行窗口
    process.Close();
    C#中隐式操作CMD命令行窗口
    //卸载完毕
    1C#中隐式操作CMD命令行窗口 //用msiexec.exe执行一个安装程序
    2C#中隐式操作CMD命令行窗口 string fileName = setupPath + "Installer.msi"; 3C#中隐式操作CMD命令行窗口 Process p = new Process(); 4C#中隐式操作CMD命令行窗口 p.StartInfo.FileName = "msiexec.exe"; 5C#中隐式操作CMD命令行窗口 p.StartInfo.Arguments = string.Format(" /i {0} /passive", fileName); 6C#中隐式操作CMD命令行窗口 p.StartInfo.UseShellExecute = false; 7C#中隐式操作CMD命令行窗口 p.StartInfo.RedirectStandardInput = true; 8C#中隐式操作CMD命令行窗口 p.StartInfo.RedirectStandardOutput = true; 9C#中隐式操作CMD命令行窗口 p.StartInfo.CreateNoWindow = false; 10C#中隐式操作CMD命令行窗口 p.Start();
  • 相关阅读:
    dragloader.js帮助你在页面原生滚动下实现Pull Request操作
    移动Web单页应用开发实践——实现Pull to Request(上/下拉请求操作)
    【转】移动Web单页应用开发实践——页面结构化
    前端网址收集!Amazing! 神奇!
    前端代码书写规范
    扫描工具 分析
    backtrack5渗透 笔记
    Sqlmap注入技巧集锦
    nmap 高级扫描用法
    小温谈数据库优化--数据设计篇(转)
  • 原文地址:https://www.cnblogs.com/LiaoHao/p/3309669.html
Copyright © 2020-2023  润新知