C#执行cmd [转载]

网址:http://www.cnblogs.com/190196539/archive/2011/12/20/2294163.html

    string cmdtext = "net use u: \\\\192.168.0.44\\src \"" + user + "\" /user:" + pwd;
            Process MyProcess = new Process();
            //设定程序名
            MyProcess.StartInfo.FileName = "cmd.exe";
            //关闭Shell的使用
            MyProcess.StartInfo.UseShellExecute = false;
            //重定向标准输入
            MyProcess.StartInfo.RedirectStandardInput = true;
            //重定向标准输出
            MyProcess.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            MyProcess.StartInfo.RedirectStandardError = true;
            //设置不显示窗口
            MyProcess.StartInfo.CreateNoWindow = true;
            //执行VER命令
            MyProcess.Start();
            MyProcess.StandardInput.WriteLine(cmdtext);
            MyProcess.StandardInput.WriteLine("exit");
            //从输出流获取命令执行结果,
            //string exepath = Application.StartupPath;
            //把返回的DOS信息读出来
            String StrInfo = MyProcess.StandardOutput.ReadToEnd();
            Console.WriteLine(StrInfo);

posted on 2013-04-22 08:18  蓝蓝的天2016  阅读(146)  评论(0编辑  收藏  举报