静默运行CMD [英] Run CMD Silently

查看:429
本文介绍了静默运行CMD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以静默方式运行CMD,但是每次遇到错误时,我都会这样做.有人可以告诉我我要去哪里了吗?

I'm trying to run CMD silently, but each time I get an error. Could someone please tell me where I'm going wrong?

Dim myProcess As Process 
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 
myProcess.StartInfo.CreateNoWindow = True 
myProcess.StartInfo.FileName = ("cmd.exe" & CmdStr) 
myProcess.Start() 

CmdStr已经是一个字符串,可以执行我想要在应用程序中执行的某些操作.

CmdStr is already a string to do certain things that I want in the application.

推荐答案

我想您的cmdStr是带有CMD参数的字符串.
如果是这样,则需要使用StartInfo的Arguments属性.
您将在myProcess变量上获得Null异常,因为它永远不会被new实例化. 您可以创建要与静态Process.Start方法一起使用的ProcessStartInfo变量,并将UseShellExecute设置为False

I suppose your cmdStr is a string with parameters for CMD.
If so you need to use the Arguments property of StartInfo.
You get a Null Exception on the myProcess variable because it is never instatiated with new. You could create a ProcessStartInfo var to use with the static Process.Start method and set the UseShellExecute to False

Dim startInfo As New ProcessStartInfo("CMD.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Hidden     
startInfo.CreateNoWindow = True 
startInfo.UseShellExecute = False
startInfo.Arguments = CmdStr
Process.Start(startInfo)  

或编辑您的代码以添加

myProcess = new Process() 

在使用var myProcess之前

before using the var myProcess

这篇关于静默运行CMD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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