C#如何实现执行CMD命令并接收返回结果

发布时间:2021-04-16 10:38:24 作者:小新
来源:亿速云 阅读:971

这篇文章主要介绍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();
        }

以上是“C#如何实现执行CMD命令并接收返回结果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Android--启动拍照功能并返回结果
  2. python脚本执行CMD命令并返回结果的例子

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

cmd命令

上一篇:SpringBoot+JavaMailSender如何实现腾讯企业邮箱配置

下一篇:Sublime Text3怎么设置快捷键打开浏览器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》