簡體   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