C#操作cmd

C#经常操作CMD,使用的话就用下面的2和3进行整理一下使用吧。

1、简单的调用命令不需要回传数据,最简单

public static void ipcmd(object p)
        {
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c " + p;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;
            process.StartInfo = startInfo;
            startInfo.Verb = "RunAs";
            process.Start();
        }

2、有回传数据的

public static string CmdExcute(string cmdStr)
        {
            Process process = new Process();
            string output = "";

            IntPtr ptr = new IntPtr();
            bool bOS_X64 = System.Environment.Is64BitOperatingSystem;
            if (bOS_X64)
            {
                Win32.Wow64DisableWow64FsRedirection(ref ptr);        // 关闭system32文件重定向
            }

            try
            {
                process.StartInfo.FileName = @"cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;

                if (process.Start())//开始进程
                {
                    process.StandardInput.AutoFlush = true;
                    process.StandardInput.WriteLine(cmdStr);
                    process.StandardInput.WriteLine("exit");
                    output = process.StandardOutput.ReadToEnd();
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (process != null)
                {
                    process.Close();
                }
            }
            if (bOS_X64)
            {
                Win32.Wow64RevertWow64FsRedirection(ptr);               //恢复文件重定向
            }
            return output;
        }
    }

3、截取输出流的

public static List<string> cmd(string p)
        {
            List<string> lines = new List<string>();
            List<string> lineNet = new List<string>();
            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c " + p;
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardInput = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.CreateNoWindow = true;
            process.StartInfo = startInfo;
            startInfo.Verb = "RunAs";

            try
            {
                process.Start();
                System.IO.StreamReader reader = process.StandardOutput;//截取输出流                

                while (!reader.EndOfStream)
                {
                    //if (reader.ReadLine().ToString().Contains("已禁用") || reader.ReadLine().ToString().Contains("已启用") || reader.ReadLine().ToString().Contains("Enabled") || reader.ReadLine().ToString().Contains("Disabled"))
                    //{
                        lineNet.Add(reader.ReadLine());
                    //}
                }
                foreach (var item in lineNet)
                {
                    if (item.Contains("已禁用") || item.Contains("已启用") || item.Contains("Disabled") || item.Contains("Enabled"))
                    {
                        lines.Add(item);
                    }
                }
            }
            catch (Exception e)
            {

            }
            finally
            {
                if (process != null)
                {
                    process.Close();
                }
            }
            return lines;
        }

转载于:https://www.cnblogs.com/javier520/p/10671549.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值