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

发布于 2024-07-06 11:31:55 字数 215 浏览 10 评论 0原文

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

有一个插件可以访问 VBScript 文件 在视觉工作室中。 但我需要访问后面代码中的脚本。 这个怎么做?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十级心震 2024-07-13 11:31:55

以下代码将执行 VBScript 脚本,没有提示或错误,也没有 shell 徽标。

System.Diagnostics.Process.Start(@"cscript //B //Nologo c:\scripts\vbscript.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 属性将使您能够非常精细地访问流程设置。

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

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

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

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 :)

提笔书几行 2024-07-13 11:31:55

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

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

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

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.

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

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

酒中人 2024-07-13 11:31:55

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

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.

谁对谁错谁最难过 2024-07-13 11:31:55

您的意思是您尝试从 C# 运行 vbs 文件?

可以像 运行任何命令来完成来自 C# 代码的其他程序

Process.Start(path);

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

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

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

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

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");
萧瑟寒风 2024-07-13 11:31:55

为了搜索者的利益,我发现了这个 post,它给出了明确的答案(特别是如果您有参数)。 已经测试过 - 似乎工作正常。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文