文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>用C#应用中执行一个控制台命令 - code snippet

用C#应用中执行一个控制台命令 - code snippet

时间:2010-11-03  来源:congrats

下面是一个很实用的程序片段,可以通过这个函数启动执行一个控制台的命令。

        /// <summary>
        /// Runs a command with supplied araguments.
        /// </summary>
        /// <param name="command"></param>
        /// <param name="arguments"></param>
        private void runShellCommand(String command, String arguments)
        {
            Process process = new Process();
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.FileName = command;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
            process.ErrorDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            while (process.HasExited == false)
            {
                Thread.Sleep(1000);
                if (ApplicationStatus.IsAborting == true)
                {
                    process.Kill();
                    break;
                }
            }
        }
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载