C#创建脚本执行不显示cmd窗口的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37137902/article/details/86483912

做一个通过Winform窗体控制服务安装卸载的功能,执行bat文件的时候显示cmd窗口,查资料得知调用vbs文件中执行可以不显示cmd窗口,遂记录下几个方法.

C#代码 


        private void tsm_installtuill_Click(object sender, EventArgs e)
        {            
            string batName = "installutilRMUSer.bat";
            string vbsName = "installutil.vbs";
            CreationBat(batName);
            ExecuteVbs(vbsName);
        }

        private void tsm_uninstalltuill_Click(object sender, EventArgs e)
        {
            string batName = "uninstallutilRMUSer.bat";
            string vbsName = "uninstallutil.vbs";
            string u = " /u ";
            CreationBat(batName, u);

            ExecuteVbs(vbsName);
        }


        #region 安装卸载服务

        /// <summary>
        /// 创建bat文件
        /// </summary>
        /// <param name="batName"></param>
        /// <param name="u"></param>
        private void CreationBat(string batName, string u = null)
        {
            string filepath = System.IO.Directory.GetCurrentDirectory();
            string batpath = filepath + @"\" + batName;

            filepath = GetParentDirectory(filepath, 2);
            filepath += @"\Service\bin\Debug\Service.exe";
            string fileContent = @"@echo off 
                                   path %SystemRoot%\Microsoft.NET\Framework\v4.0.30319
                                   @echo off 
                                   InstallUtil.exe " + u + filepath + @"
                                   pause";
            writeBATFile(fileContent, batpath);
            //string thisbatpath = System.IO.Directory.GetCurrentDirectory() + @"\";
            //ExecuteBat(batName, thisbatpath);
        }

        /// <summary>
        /// 写入bat文件
        /// </summary>
        /// <param name="fileContent"></param>
        /// <param name="filePath"></param>
        public void writeBATFile(string fileContent, string filePath)
        {
            //string filePath = "D:\\test\\testChange.bat";
            if (!File.Exists(filePath))
            {
                FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(fileContent);//开始写入值
                sw.Close();
                fs1.Close();
            }
            else
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(fileContent);//开始写入值
                sr.Close();
                fs.Close();
            }
        }

        /// <summary>
        /// 调用执行bat文件
        /// </summary>
        private void ExecuteBat(string batName, string thisbatpath)
        {
            Process proc = null;
            try
            {
                string targetDir = string.Format(thisbatpath);//this is where testChange.bat lies
                proc = new Process();
                proc.StartInfo.WorkingDirectory = targetDir;
                proc.StartInfo.FileName = batName;
                proc.StartInfo.Arguments = string.Format("10");//this is argument
                proc.StartInfo.RedirectStandardError = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
                proc.Start();
                proc.WaitForExit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
            }
        }

        /// <summary>
        /// 调用执行vbs文件
        /// </summary>
        /// <param name="vbsName">vbs文件名,含扩展名称</param>
        private void ExecuteVbs(string vbsName)
        {
            string path = System.IO.Directory.GetCurrentDirectory() + @"\"+vbsName;
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "wscript.exe";
            startInfo.Arguments = path;
            Process.Start(startInfo);
        }

        /// <summary>
        /// 获取上级文件夹路径
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="degree">级数</param>
        /// <returns></returns>
        private string GetParentDirectory(string path, int degree)
        {
            DirectoryInfo pathInfo = new DirectoryInfo(path);
            string newPath = pathInfo.Parent.FullName;

            for (int i = 0; i < degree; i++)
            {
                pathInfo = new DirectoryInfo(newPath);
                newPath = pathInfo.Parent.FullName;
            }
            return newPath;
        }

        #endregion

在和bat文件同目录下新建vbs文件,写入以下代码

set ws=WScript.CreateObject("WScript.Shell") 

ws.Run "installutil.bat",0

C#中执行调用vbs文件的方法即可隐藏cmd窗口

猜你喜欢

转载自blog.csdn.net/m0_37137902/article/details/86483912