Process.Start调用CMD

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

namespace SocketDemo {
    class Program {
        static void Main() {
            Process proc = new Process();
            proc.StartInfo.FileName = "net.exe";
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.Arguments = "view";
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;
            proc.Start();

            StreamReader sr = new StreamReader(proc.StandardOutput.BaseStream);
            string line = "";
            List<string> names = new List<string>();

            while ((line = sr.ReadLine()) != null) {
                //                if (line.StartsWith(@"\\")) {
                names.Add(line); //.Substring(2).TrimEnd());
                //                }
            }

            sr.Close();
            proc.WaitForExit();

            foreach (string name in names) {
                Console.WriteLine(name);
            }

            Console.ReadKey();
        }
    }
}

原文地址:https://www.cnblogs.com/rickiedu/p/1499794.html