热门标签 | 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);

推荐阅读
  • 本文详细介绍了GetModuleFileName函数的用法,该函数可以用于获取当前模块所在的路径,方便进行文件操作和读取配置信息。文章通过示例代码和详细的解释,帮助读者理解和使用该函数。同时,还提供了相关的API函数声明和说明。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • 31.项目部署
    目录1一些概念1.1项目部署1.2WSGI1.3uWSGI1.4Nginx2安装环境与迁移项目2.1项目内容2.2项目配置2.2.1DEBUG2.2.2STAT ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
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社区 版权所有