自动编译.net项目

        /// <summary>
        /// 自动编译.net项目
        /// </summary>
        /// <param name="proccessMsg">返回处理进程消息</param>
        /// <returns>返回是否成功</returns>
        public static bool VsAutoComplie(IList<string> proccessMsg)
        {
            string processPath =  @"D:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe";
            string args = @" E:\work\CRMProject\QZH_CRM\QZH_CRM.sln /build Debug /out complile.log";
            string workingDirectory = @"E:\Work\Crm\Web";
            int code = StartProcess(proccessMsg, processPath, args, workingDirectory);
            return code == 0;
        }

        /// <summary>
        /// 执行外部程序
        /// </summary>
        /// <param name="proccessMsg">返回处理进程消息</param>
        /// <param name="processPath">设置外部程序名(包括程序路径)</param>
        /// <param name="args">设置外部程序的启动参数</param>
        /// <param name="workingDirectory">设置外部程序工作目录</param>
        /// <returns>外部程序在结束运行时的返回值</returns>
        public static int StartProcess(IList<string> proccessMsg, string processPath, string args, string workingDirectory)
        {
            //args = @" E:\Work\Crm\Crm.sln /build Debug /out complile.log";
            int WaitForExitInMilliseconds = 10 * 60 * 1000;//等待时间

            //声明一个程序信息类
            System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();

            //设置外部程序名
            Info.FileName = processPath;

            //设置外部程序的启动参数(命令行参数)
            Info.Arguments = args;

            //设置外部程序工作目录
            Info.WorkingDirectory = workingDirectory;

            //声明一个程序类
            System.Diagnostics.Process Proc = new System.Diagnostics.Process();

            try
            {
                //启动外部程序
                Proc = System.Diagnostics.Process.Start(Info);
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                proccessMsg.Add(string.Format("系统找不到指定的程序文件。{0} ", System.Environment.NewLine + ex));
            }
            finally
            {

                //打印出外部程序的开始执行时间
                proccessMsg.Add(string.Format("外部程序的开始执行时间:{0} ", Proc.StartTime));

                //等待时间
                Proc.WaitForExit(WaitForExitInMilliseconds);

                //如果这个外部程序没有结束运行则对其强行终止
                if (Proc.HasExited == false)
                {
                    proccessMsg.Add("由主程序强行终止外部程序的运行! ");
                    Proc.Kill();
                }
                else
                {
                    proccessMsg.Add("由外部程序正常退出! ");
                }
                proccessMsg.Add(string.Format("外部程序的结束运行时间:{0} ", Proc.ExitTime));
                proccessMsg.Add(string.Format("外部程序在结束运行时的返回值:{0} ", Proc.ExitCode));
            }
            return Proc.ExitCode;
        }

posted @ 2011-12-30 18:35  zzljh  阅读(360)  评论(0编辑  收藏  举报