[英]Execute VBS Script With Command Prompt Arguments in C#
I have a .vbs script file that needs to be executed from a C# application. 我有一个.vbs脚本文件,需要从C#应用程序执行。 We would normally execute the vbs file from right-clicking it and selecting "Open With Command Prompt" so the user can input arguments and the script will take off. 通常,我们将通过右键单击vbs文件并选择“使用命令提示符打开”来执行vbs文件,以便用户可以输入参数,然后脚本将开始运行。
With the code below I'm able to execute the vbs file but it still prompts for input: 使用下面的代码,我可以执行vbs文件,但仍提示输入:
var MyProcess = new Process();
MyProcess.StartInfo.FileName = @"MyVBSScript.vbs";
MyProcess.StartInfo.WorkingDirectory = @"C:\Folder\WhereVBS\FileLives";
MyProcess.StartInfo.Arguments = @"UserArgumentWithoutSpaces";
MyProcess.Start();
MyProcess.WaitForExit();
MyProcess.Close();
My goal is to bypass the prompt by passing an argument. 我的目标是通过传递参数来绕过提示。 Is there something I need to do in the VBS file or does something in my C# code need to change? 我需要在VBS文件中做些什么,还是需要更改C#代码中的某些内容?
I'm not sure what the args are that you want to pass in but look at my HelloWorld example below. 我不确定您要传递的参数是什么,但是请看下面我的HelloWorld示例。 The args I have in this script are /admin
or /user
and a Case...Else
to ensure that the script cannot be run without args. 我在此脚本中使用的args是/admin
或/user
以及Case...Else
以确保没有args无法运行该脚本。 The commandline would be cscript.exe "C:\\Scripts\\Hello_with_Args.vbs" /admin
if you want the process to be somewhat hidden and wscript.exe "C:\\Scripts\\Hello_with_Args.vbs" /admin
if you want the user to see it. 如果您希望将进程隐藏起来,则命令行为cscript.exe "C:\\Scripts\\Hello_with_Args.vbs" /admin
如果希望用户执行以下操作,则命令行为wscript.exe "C:\\Scripts\\Hello_with_Args.vbs" /admin
看见。 and use the MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
并使用MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
or something like that to hide the command prompt window. 或类似的东西来隐藏命令提示符窗口。 Hope this helps. 希望这可以帮助。
'Hello_with_Args.vbs
Dim args
Set args = WScript.Arguments
If args.Count > 0 Then
For i = 0 To args.Count - 1
Select Case LCase(args.Item(i))
Case "/admin"
WScript.Echo "Hello World!!" & vbCrLf & "You passed the /admin arg."
Case "/user"
WScript.Echo "Hello World!!" & vbCrLf & "You passed the /user arg."
Case Else
WScript.Echo "You can only use the ""/admin"" or ""/user"" command line arg or do not specify an arg."
End Select
Next
Else
Wscript.Echo "Hello World!!" & vbCrLf & "No command line args passed."
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.