热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在c#应用程序中调用VBScript文件?-HowtocallaVBScriptfileinaC#application?

IneedtocallaVBScriptfile(.vbsfileextension)inmyC#Windowsapplication.HowcanIdothis

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

我需要调用Vbscript文件(。vbs文件扩展名)在我的c# Windows应用程序中。我该怎么做呢?

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文件。但是我需要访问后面的代码。如何做到这一点呢?

5 个解决方案

#1


58  

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

下面的代码将执行一个没有提示或错误、没有shell标志的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 StartInfo properties will give you quite granular access to the process settings.

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

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

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

#2


4  

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类文件,并引用VB。NET类库来自您的c#程序。

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

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

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

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

#3


3  

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年的最高权限级别上运行。标识必须是管理员。

#4


2  

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

#5


1  

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

推荐阅读
author-avatar
惠君宛峰6
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有