从 C# 调试 vbscript [英] Debugging a vbscript from C#
本文介绍了从 C# 调试 vbscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.WorkingDirectory = @"C:\MyPath\";
scriptProc.StartInfo.Arguments = "filename.vbs //X";
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();
我的 VBS 在由//X 属性指定的编辑器(Visual Studio)中打开,但这仅在脚本没有语法错误时打开,如果我有脚本错误,则不会在编辑器中打开,基本上使调试器的使用变得多余.
My VBS opens in an editor(Visual Studio) which is specified by the //X attribute, but this only opens if the script has no syntax errors, it is not opening in the editor if I have script errors, which basically makes the use of the debugger as redundant.
有什么方法可以让我仅使用 C# 调试 VBScript?
Is there any way with which I can debug a VBScript using C# only?
推荐答案
下面的代码使用 @Ekkehard.Horner 方法.编译它,然后将 .vbs 文件拖放到可执行文件上以测试文件是否有语法错误:
The code below uses @Ekkehard.Horner approaches. Compile it, then drag and drop .vbs files onto executable to test whether the file has syntax errors or not:
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
// Add reference to COM Microsoft Script Control 1.0
// Code works for .Net 2.0 and above
class Program
{
static void Main(string[] args)
{
// Check whether a file was dragged onto executable
if (args.Length != 1)
{
MessageBox.Show("Drag'n'drop .vbs file onto this executable to check syntax");
return;
}
MessageBox.Show("Syntax will be checked for\r\n" + args[0]);
String vbscode = "";
// Read the content of the file
try
{
StreamReader sr = new StreamReader(args[0]);
vbscode = sr.ReadToEnd();
}
catch (Exception e)
{
MessageBox.Show("File reading error " + e.Message);
return;
}
// Add statement raising runtime error -2147483648 in the first line to ScriptControl
int hr = 0;
try
{
vbscode = "Err.Raise &H80000000\r\n" + vbscode;
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
sc.AddCode(vbscode);
}
catch (Exception e)
{
hr = Marshal.GetHRForException(e);
// First line of code executed if no syntax errors only
if (hr == -2147483648)
{
// Run time error -2147483648 shows that execution started without syntax errors
MessageBox.Show("Syntax OK");
}
else
{
// Otherwise there are syntax errors
MessageBox.Show("Syntax error");
}
}
}
}
这篇关于从 C# 调试 vbscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文