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

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

问题描述

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

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

有一个加载项可以访问 VBScript 文件在 Visual Studio 中.但我需要在后面的代码中访问脚本.如何做到这一点?

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?

推荐答案

以下代码将执行一个 VBScript 脚本,没有提示或错误,也没有 shell 标志.

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");

使用更复杂的技术:

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 Script Host脚本程序.您也可以尝试直接执行 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天全站免登陆