.NET启动CMD执行(转载)

简介: string str = Console.ReadLine(); System.Diagnostics.Process p = new System.
string str = Console.ReadLine();

        System.Diagnostics.Process p = new System.Diagnostics.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.Start();//启动程序

        //向cmd窗口发送输入信息
        p.StandardInput.WriteLine(str + "&exit");

        p.StandardInput.AutoFlush = true;
        //p.StandardInput.WriteLine("exit");
        //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
        //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令



        //获取cmd窗口的输出信息
        string output = p.StandardOutput.ReadToEnd();

        //StreamReader reader = p.StandardOutput;
        //string line=reader.ReadLine();
        //while (!reader.EndOfStream)
        //{
        //    str += line + "  ";
        //    line = reader.ReadLine();
        //}

        p.WaitForExit();//等待程序执行完退出进程
        p.Close();


        Console.WriteLine(output);
目录
相关文章
|
.NET C++ 开发框架
ASP.NET CORE下运行CMD命令
ASP.NET CORE下运行CMD命令,用以前的ASP.NET 的命令System.Diagnostics.Process.Start("notepad");这样是可以运行出记事本的, 现在公司的C++大神开发了个EXE,需要放在服务器上,然后当访问服务器上的某个网页的时候就执行这个EXE了。
2018 0
|
Shell Windows
.NET 调用CMD执行
using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Windows\System32\cmd.
1011 0
|
移动开发 Shell Windows
.Net执行cmd获取系统信息
public string Test() { Stopwatch w = new Stopwatch(); w.
614 0
|
29天前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
28 7
|
27天前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
39 0
|
2月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
36 0
|
2月前
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
|
2月前
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
94 0
|
5月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
159 0
下一篇
无影云桌面