首页 > 编程语言 > C# 执行CMD命令并接收返回结果的操作方式
2021
05-02

C# 执行CMD命令并接收返回结果的操作方式

最近工作的时候发现软件里面通过查询ARP表查询某一IP对应的ARP条目的时,概率性出现查询到的ARP条目为空,一开始怀疑Ping通但是没有学习到ARP,

后来想想这是不可能的,最后经过各种分析发现是软件中调用清除ARP的操作是通过调用Kernel.dll中的WinExec实现的,这个函数只要调用成功即返回,并不会等待调用的程序执行完毕才返回,

所以在某些反应迟钝的电脑上,就会出现:如果你的操作顺序是清除ARP,Ping,查询ARP,就可能出现在Ping完后ARP表被清除掉,导致查不到ARP条目。

在网上查询C#调用程序并等待程序执行完毕才返回的实现方法如下:

1.引入

using System.Diagnostics;  

2.创建执行CMD

Process CmdProcess = new Process();  
CmdProcess.StartInfo.FileName = "cmd.exe";    

3.配置开发方式输入输出错误

CmdProcess.StartInfo.CreateNoWindow = true;         // 不创建新窗口    
CmdProcess.StartInfo.UseShellExecute = false;       //不启用shell启动进程  
CmdProcess.StartInfo.RedirectStandardInput = true;  // 重定向输入    
CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出    
CmdProcess.StartInfo.RedirectStandardError = true;  // 重定向错误输出  

4.执行cmd且获取返回值

方法一

CmdProcess.StartInfo.Arguments = "/c " + "=====cmd命令======";//“/C”表示执行完命令后马上退出  
CmdProcess.Start();//执行 
CmdProcess.StandardOutput.ReadToEnd();//获取返回值   
CmdProcess.WaitForExit();//等待程序执行完退出进程   
CmdProcess.Close();//结束 

方法二

CmdProcess.StandardInput.WriteLine(str + "&exit"); //向cmd窗口发送输入信息  
CmdProcess.StandardInput.AutoFlush = true;  //提交  
CmdProcess.Start();//执行   
CmdProcess.StandardOutput.ReadToEnd();//输出   
CmdProcess.WaitForExit();//等待程序执行完退出进程   
CmdProcess.Close();//结束  

5.输出返回值

首先 引入

using System.IO;
 
            StreamReader sr =CmdProcess.StandardOutput;//获取返回值 
            string line = ""; 
            int num = 1;
            while ((line=sr.ReadLine())!=null)
            {   
                if(line!="")
                {
                    Console.WriteLine(line + " " + num++);
                }
            }

6.Process 的 HasExited属性

    //等待程序执行完退出进程
    CmdProcess.WaitForExit();
 
    //判断程序是退出了进程 退出为true(上面的退出方法执行完后,HasExited的返回值为 true) 
     falg = CmdProcess.HasExited; 

补充:C# 动态调用exe可执行程序并接受返回值

看代码吧~

        static void Main(string[] args)
        {
            object output;
            try
            {
                using (Process p = new Process())
                {
                    p.StartInfo.FileName = @"ConsoleApp2.exe";//可执行程序路径
                    p.StartInfo.Arguments = "";//参数以空格分隔,如果某个参数为空,可以传入""
                    p.StartInfo.UseShellExecute = false;//是否使用操作系统shell启动
                    p.StartInfo.CreateNoWindow = true;//不显示程序窗口
                    p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
                    p.StartInfo.RedirectStandardInput = true;   //接受来自调用程序的输入信息
                    p.StartInfo.RedirectStandardError = true;   //重定向标准错误输出
                    p.Start();
                    p.WaitForExit();
                    //正常运行结束放回代码为0
                    if (p.ExitCode != 0)
                    {
                        output = p.StandardError.ReadToEnd();
                        output = output.ToString().Replace(System.Environment.NewLine, string.Empty);
                        output = output.ToString().Replace("\n", string.Empty);
                        throw new Exception(output.ToString());
                    }
                    else
                    {
                        output = p.StandardOutput.ReadToEnd();
                    }
                }
                Console.WriteLine(output);
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
            }
            Console.ReadKey();
        }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。如有错误或未考虑完全的地方,望不吝赐教。

编程技巧