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();
        }
    }
}

posted @ 2009-06-09 16:55  飞扬跋扈  阅读(652)  评论(0编辑  收藏  举报