【编程技巧】执行DOS命令

1,605次阅读
一条评论

共计 2434 个字符,预计需要花费 7 分钟才能阅读完成。

C++可以通过[code]system()[/code]执行系统命令,那么C#如何执行系统命令呢?
【编程技巧】执行DOS命令

步骤

C#并没有直接提供执行系统命令的函数,但是我们可以通过静默启动cmd实现。注:请先引用命名空间[code]using System.Diagnostics;[/code]1.初始化cmd进程对象
//创建进程对象
Process process = new Process();
//设定此进程对象的信息
ProcessStartInfo startInfo = new ProcessStartInfo();
//设定将要打开的应用
startInfo.FileName = System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\cmd.exe";
//设定需要执行的命令,“/C”表示执行完命令后马上退出  
startInfo.Arguments = "/C " + "执行的命令";
//不使用系统外壳程序启动
startInfo.UseShellExecute = false;
//不重定向输入
startInfo.RedirectStandardInput = false;
//重定向输出(用于读取输出)
startInfo.RedirectStandardOutput = true;
//不创建窗口,不显示
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
2. 打开cmd,读取输入,并退出
try
{
    //启动程序
    process.Start();
    //无限等待退出
    process.WaitForExit();
    //获得输出信息
    string outstr = process.StandardOutput.ReadToEnd();
}
finally
{
    process.Close();
}

实例

读取输入的命令,执行后输出结果
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CMD
{
    class Program
    {
        //创建进程对象
        static string[] c = { "1", "1" };
        static Process process = new Process();
        static void Main(string[] args)
        {
            if (args != c)
            {
                //设置信息
                Console.Beep(4000, 500);
                Console.Title = "CMD模拟";
                Console.WriteLine("[ 控制台模拟 Ver=1.0 Author=C#之家 Website=https://www.chsarphome.xyz ]");
            }
            Console.Write("->");
            //设定此进程对象的信息
            ProcessStartInfo startInfo = new ProcessStartInfo();
            //设定将要打开的应用
            startInfo.FileName = System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\cmd.exe";
            //设定需要执行的命令,“/C”表示执行完命令后马上退出  
            startInfo.Arguments = "/C " + Console.ReadLine();
            if (startInfo.Arguments == "/C exit") 
            {
                Environment.Exit(0);
            }
            if (startInfo.Arguments == "/C cls")
            {
                Console.Clear();
                Main(c);
            }
            if (startInfo.Arguments == "/C ver")
            {
                Console.WriteLine("[ 控制台模拟 Ver=1.0 Author=C#之家 Website=https://www.chsarphome.xyz ]");
                Main(c);
            }
            //不使用系统外壳程序启动
            startInfo.UseShellExecute = false;
            //不重定向输入
            startInfo.RedirectStandardInput = false;
            //重定向输出(用于读取输出)
            startInfo.RedirectStandardOutput = true;
            //不创建窗口,不显示
            startInfo.CreateNoWindow = true;
            process.StartInfo = startInfo;
            try
            {
                //启动程序
                process.Start();
                //无限等待退出
                process.WaitForExit();
                //获得输出信息
                string outstr = process.StandardOutput.ReadToEnd();
                Console.WriteLine(outstr);
            }
            finally
            {
                process.Close();
            }
            Main(c);
        }
    }
}
正文完
 
lvshujun
版权声明:本站原创文章,由 lvshujun 2019-02-08发表,共计2434字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请联系站长并注明出处。
评论(一条评论)