如何在 C# 应用程序中调用 VBScript 文件?
我需要在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下代码将执行 VBScript 脚本,没有提示或错误,也没有 shell 徽标。
更复杂的技术是使用:
使用
StartInfo
属性将使您能够非常精细地访问流程设置。如果你想让脚本程序显示窗口等,你需要使用Windows Script Host 。 您也可以尝试直接执行
cscript
但在某些系统上它只会启动编辑器:)The following code will execute a VBScript script with no prompts or errors and no shell logo.
A more complex technique would be to use:
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 :)另一种方法是创建 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.
这是一个权限问题。 您的应用程序 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.
您的意思是您尝试从 C# 运行 vbs 文件?
可以像 运行任何命令来完成来自 C# 代码的其他程序:
但是您必须确保它不会要求任何内容,并且它正在使用解释器的命令行版本运行:
You mean you try to run a vbs file from C#?
It can be done like running any other program from C# code:
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:
为了搜索者的利益,我发现了这个 post,它给出了明确的答案(特别是如果您有参数)。 已经测试过 - 似乎工作正常。
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.