如何调用C#应用程序VBScript文件?应用程序、文件、VBScript

2023-09-02 10:18:17 作者:Spume‘浅爱ゝ

我需要调用一个的VBScript 文件(.vbs文件扩展名)在我的C#Windows应用程序。 我怎样才能做到这一点?

I need to call a VBScript file (.vbs file extension) in my C# Windows application. How can I do this?

还有一个附加的访问VBScript文件 在Visual Studio中。 但我需要访问脚本code后面。如何做到这一点?

There is an add-in to access a VBScript file in Visual Studio. But I need to access the script in code behind. How to do this?

推荐答案

以下code将执行一个VBScript脚本,没有提示或错误,没有壳的标志。

The following code will execute a VBScript script with no prompts or errors and no shell logo.

System.Diagnostics.Process.Start(@"cscript //B //Nologo c:scriptsvbscript.vbs");

一个更复杂的方法是使用:

A more complex technique would be to use:

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript"; 
scriptProc.StartInfo.WorkingDirectory = @"c:scripts"; //<---very important 
scriptProc.StartInfo.Arguments ="//B //Nologo vbscript.vbs";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
scriptProc.Start();
scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit
scriptProc.Close();

使用的StartInfo 属性会给你非常细粒度的访问的过程中设置。

Using the StartInfo properties will give you quite granular access to the process settings.

您需要使用 Windows脚本宿主如果你想窗等由脚本程序来显示。你也可以尝试只执行 CSCRIPT 直接,但在一些系统上它只是启动编辑器:)

You need to use Windows Script Host if you want windows, etc. to be displayed by the script program. You could also try just executing cscript directly but on some systems it will just launch the editor :)