如何运行 Windows 批处理文件但隐藏命令窗口? [英] How can I run a windows batch file but hide the command window?

查看:32
本文介绍了如何运行 Windows 批处理文件但隐藏命令窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何运行 Windows 批处理文件但隐藏命令窗口?我不希望 cmd.exe 在执行文件时在屏幕上可见.这可能吗?

How can I run a windows batch file but hiding the command window? I dont want cmd.exe to be visible on screen when the file is being executed. Is this possible?

推荐答案

如果您编写一个非托管程序并使用 CreateProcess API 然后你应该初始化 lpStartupInfo 类型的参数 STARTUPINFO 以便 wShowWindow 字段结构是 SW_HIDE 和不要忘记在 STARTUPINFO.另一种方法是使用 CREATE_NO_WINDOW 标志dwCreationFlags 参数.同样的技巧也适用于 ShellExecuteShellExecuteEx 函数.

If you write an unmanaged program and use CreateProcess API then you should initialize lpStartupInfo parameter of the type STARTUPINFO so that wShowWindow field of the struct is SW_HIDE and not forget to use STARTF_USESHOWWINDOW flag in the dwFlags field of STARTUPINFO. Another method is to use CREATE_NO_WINDOW flag of dwCreationFlags parameter. The same trick work also with ShellExecute and ShellExecuteEx functions.

如果您编写托管应用程序,您应该遵循 http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx:用<初始化ProcessStartInfocode>CreateNoWindow = true 和 UseShellExecute = false 然后用作 .就像您可以将 ProcessStartInfo 的属性 WindowStyle 设置为 ProcessWindowStyle.Hidden 或与 CreateNoWindow = true 一起设置的情况>.

If you write a managed application you should follows advices from http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx: initialize ProcessStartInfo with CreateNoWindow = true and UseShellExecute = false and then use as a parameter of . Exactly like in case of you can set property WindowStyle of ProcessStartInfo to ProcessWindowStyle.Hidden instead or together with CreateNoWindow = true.

可以使用以 wcsript.exe 开头的 VBS 脚本.在脚本中,您可以使用 CreateObject("WScript.Shell"),然后使用 运行,将 0 作为第二个 (intWindowStyle) 参数.请参阅 http://www.robvanderwoude.com/files/runnhide_vbs.txt 作为例子.我可以继续使用 Kix,PowerShell 等等.

You can use a VBS script which you start with wcsript.exe. Inside the script you can use CreateObject("WScript.Shell") and then Run with 0 as the second (intWindowStyle) parameter. See http://www.robvanderwoude.com/files/runnhide_vbs.txt as an example. I can continue with Kix, PowerShell and so on.

如果您不想编写任何程序,您可以使用任何现有实用程序,例如 CMDOW/RUN/HID "c:SomeDirMyBatch.cmd", hstart/NOWINDOW/D=c:scripts "c:scriptsmybatch.bat", hstart/NOCONSOLEbatch_file_1.bat" 做的完全一样.我相信您会发现更多此类免费实用程序.

If you don't want to write any program you can use any existing utility like CMDOW /RUN /HID "c:SomeDirMyBatch.cmd", hstart /NOWINDOW /D=c:scripts "c:scriptsmybatch.bat", hstart /NOCONSOLE "batch_file_1.bat" which do exactly the same. I am sure that you will find much more such kind of free utilities.

在某些情况下(例如从 UNC 路径开始),将工作目录也设置为某个本地路径(%SystemRoot%system32 始终工作).这对于使用上面列出的启动隐藏批处理的任何变体都很重要.

In some scenario (for example starting from UNC path) it is important to set also a working directory to some local path (%SystemRoot%system32 work always). This can be important for usage any from above listed variants of starting hidden batch.

这篇关于如何运行 Windows 批处理文件但隐藏命令窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆