Windows系统隐藏窗口启动控制台程序

背景

上线项目有时候需要一些控制台应用作为辅助服务来协助UI应用满足实际需求,这时候如果一运行UI就冒出一系列的黑框,这将会导致客户被下的不起,生怕中了什么不知名病毒

方案

可以使用vbs来启动,这个是window系统自带的,环境早已自动配置好,编写完代码即可使用

格式如下:

vbscript 复制代码
Set wshshell=CreateObject("WScript.Shell")
wshshell.Run "cmd /c 我们的bat或者cmd命令",0
Set wshshell=Nothing

WScript.Shell对象的Run方法中,第二个参数设置为0,就表示隐藏窗口启动

举例

如果我们想要执行一个命令,执行后在D盘创建一个文件1.txt,并把内容hello输入到1.txt里面。我们就可以编写如下vbs代码

vbscript 复制代码
Set wshshell=CreateObject("WScript.Shell")
wshshell.Run "cmd /c D: & echo hello > 1.txt ",0
Set wshshell=Nothing

编写完后保存为1.vbs,然后双击鼠标左键执行。

执行前

执行后,产生了1.txt,打开可以看到内容也是我们写入的hello,并且启动的时候没有任何弹窗或者闪屏

相关推荐
DogDaoDao4 天前
Windows 双 Shell 全面对比:CMD 与 PowerShell 的技术剖析
windows·shell·测试·powershell·cmd·程序员开发
Vex2une19 天前
Windows PowerShell 发展史:从命令行工具到自动化核心
windows·自动化运维·命令行·powershell·cmd·脚本语言·对象管道
fox_charon1 个月前
Windows 下 CLI 参数的引号陷阱:为什么 --resume 'uuid' 会失败
windows·shell·cmd·cli·引号
五_谷_丰_登1 个月前
windows批处理脚本(bat)问题解决方案大全——持续更新
windows·bat
椰羊~王小美2 个月前
8080端口被占用,怎么cmd杀死
cmd
鬼手点金3 个月前
Hermes‑Agent 使用教程
python·ubuntu·powershell·cmd·ai agent·opencode·hermes
儒雅的烤地瓜3 个月前
计算机网络 | 网络诊断双刃剑:Tracert路由追踪实战与Windows防火墙端口配置指南
tcp/ip·计算机网络·wireshark·防火墙·powershell·cmd
生活、追梦者3 个月前
【Windows】 CMD 黑窗口命令大全
windows·命令行·cmd·运维技巧
winfredzhang4 个月前
Python + wxPython + SQLite 实战:开发一个本地 Python 项目一键启动管理工具
python·sqlite·bat·截图·claudecode·codingliteplan
Uarebaby.c5 个月前
CMD和PowerShell的核心区别
cmd