zoukankan      html  css  js  c++  java
  • .NET&C# 执行cmd命令,并取得结果

    代码:

    private static string CMDPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\cmd.exe";
    
     public static void RunCMDCommand(string Command, out string OutPut)
            {
                using (Process pc = new Process())
                {
                    Command = Command.Trim().TrimEnd('&') + "&exit";
    
                    pc.StartInfo.FileName = CMDPath;
                    pc.StartInfo.CreateNoWindow = true;
                    pc.StartInfo.RedirectStandardError = true;
                    pc.StartInfo.RedirectStandardInput = true;
                    pc.StartInfo.RedirectStandardOutput = true;
                    pc.StartInfo.UseShellExecute = false;
    
                    pc.Start();
    
                    pc.StandardInput.WriteLine(Command);
                    pc.StandardInput.AutoFlush = true;
    
                    OutPut = pc.StandardOutput.ReadToEnd();
                    int P = OutPut.IndexOf(Command) + Command.Length;
                    OutPut = OutPut.Substring(P, OutPut.Length - P - 3);
                    pc.WaitForExit();
                    pc.Close();
                }
            }

    执行:

    string result = string.Empty;
    RunCMDCommand("ipconfig /all",out result);

    结果:

    转载:https://www.cnblogs.com/bqh10086/p/11940454.html

  • 相关阅读:
    POST
    界面,数据下载
    异步下载
    Cell
    循环&信息添加&颜色修改
    通讯录
    图片循环
    多删搜索
    图片滚动
    TableView
  • 原文地址:https://www.cnblogs.com/JourneyOfFlower/p/14345586.html
Copyright © 2011-2022 走看看