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

做一个通过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窗口

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值