繁体   English   中英

在C#中使用命令提示符参数执行VBS脚本

[英]Execute VBS Script With Command Prompt Arguments in C#

我有一个.vbs脚本文件,需要从C#应用程序执行。 通常,我们将通过右键单击vbs文件并选择“使用命令提示符打开”来执行vbs文件,以便用户可以输入参数,然后脚本将开始运行。

使用下面的代码,我可以执行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();

我的目标是通过传递参数来绕过提示。 我需要在VBS文件中做些什么,还是需要更改C#代码中的某些内容?

我不确定您要传递的参数是什么,但是请看下面我的HelloWorld示例。 我在此脚本中使用的args是/admin/user以及Case...Else以确保没有args无法运行该脚本。 如果您希望将进程隐藏起来,则命令行为cscript.exe "C:\\Scripts\\Hello_with_Args.vbs" /admin如果希望用户执行以下操作,则命令行为wscript.exe "C:\\Scripts\\Hello_with_Args.vbs" /admin看见。 并使用MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 或类似的东西来隐藏命令提示符窗口。 希望这可以帮助。

'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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM