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

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

在Visual Studio中有一个加载项来访问VBScript文件。 但是我需要在后面的代码中访问脚本。 如何做到这一点?

下面的代码将执行一个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属性可以非常细致地访问stream程设置。

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

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

你将需要修复VBScript和VB.NET之间的差异(应该很less)。

这里的优点是你可以在进程中运行代码。

这是一个权限问题。 您的应用程序appPool必须以最高的权限级别运行,以便在2008年完成此操作。身份必须是pipe理员。

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

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

 Process.Start(path); 

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

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

为了search者的利益,我发现这个post ,给出了一个明确的答案(尤其是如果你有参数)。 已经testing过 – 似乎工作正常。

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