如何调用C#应用程序VBScript文件? [英] How to call a VBScript file in a C# application?

查看:127
本文介绍了如何调用C#应用程序VBScript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个的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:\scripts\vbscript.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 :)

这篇关于如何调用C#应用程序VBScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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