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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/200422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:45:33  来源:igfitidea点击:

How to call a VBScript file in a C# application?

c#.netasp.net.net-2.0vbscript

提问by balaweblog

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

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

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?

有一个加载项可以访问 Visual Studio 中的 VBScript 文件。但是我需要在后面的代码中访问脚本。这该怎么做?

采纳答案by Ilya Kochetov

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

下面的代码将执行一个 VBScript 脚本,没有提示或错误,也没有外壳标志。

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

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

使用这些StartInfo属性将使您能够非常精细地访问流程设置。

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

如果希望脚本程序显示窗口等,则需要使用Windows Script Host。您也可以尝试直接执行cscript,但在某些系统上它只会启动编辑器:)

回答by Biri

You mean you try to run a vbs file from C#?

你的意思是你尝试从 C# 运行一个 vbs 文件?

It can be done like running any other program from C# code:

它可以像从 C# 代码运行任何其他程序一样完成:

Process.Start(path);

But you have to make sure that it won't ask for anything, and it is running with the command line version of the interpreter:

但是您必须确保它不会要求任何东西,并且它正在使用解释器的命令行版本运行:

Process.Start("cscript path\to\script.vbs");

回答by qxotk

Another approach is to create a VB.NET Class Library project, copy your VBScript code into a VB.NET Class file, and reference the VB.NET Class Library from your C# program.

另一种方法是创建一个 VB.NET 类库项目,将您的 VBScript 代码复制到一个 VB.NET 类文件中,然后从您的 C# 程序中引用 VB.NET 类库。

You will need to fix-up any differences between VBScript and VB.NET (should be few).

您需要修正 VBScript 和 VB.NET 之间的任何差异(应该很少)。

The advantage here, is that you will run the code in-process.

这里的优点是您将在进程中运行代码。

回答by Will

This is a permissions issue. Your application appPool must be running at the highest permission level to do this in 2008. The Identity must be Administrator.

这是一个权限问题。您的应用程序 appPool 必须以最高权限级别运行才能在 2008 年执行此操作。身份必须是管理员。

回答by HockeyJ

For the benefit of searchers, I found this post, which gives a clear answer (esp if you have parameters). Have tested it - seems to work fine.

为了搜索者的利益,我找到了这篇文章,它给出了明确的答案(特别是如果你有参数)。已经测试过了 - 似乎工作正常。

string scriptName = "myScript.vbs"; // full path to script
int abc = 2;
string name = "Serrgggio";

ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "cscript.exe";
ps.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", scriptName, abc, name);
//This will equate to running via the command line:
// > cscript.exe "myScript.vbs" "2" "Serrgggio"
Process.Start(ps);