C#- Winform调用BAT例子

  前段时间在工做的时候须要用到,百度了很久后找,但是找到了又但愿调用的时候窗体不要显示出来。this

  proc.StartInfo.CreateNoWindow = true;
       proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;spa

  这两句能隐藏掉CMD窗口code

 

  最后的代码以下:orm

           try
            {
                string str = System.Windows.Forms.Application.StartupPath + "\\1.bat";

                string strDirPath = System.IO.Path.GetDirectoryName(str);
                string strFilePath = System.IO.Path.GetFileName(str);

                string targetDir = string.Format(strDirPath);//this is where mybatch.bat lies
                proc = new Process();
                proc.StartInfo.WorkingDirectory = targetDir;
                proc.StartInfo.FileName = strFilePath;
                
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.Start();
                proc.WaitForExit();


                MessageBox.Show("执行成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("执行失败 错误缘由:" + ex.Message);
            }
相关文章
相关标签/搜索