使用C#执行vbscript时遇到了很大的问题。 我有这个通用的流程执行功能:
/// <summary>
/// Runs a process silent (Without DOS window) with the given arguments and returns the process' exit code.
/// </summary>
/// <param name="output">Recieves the output of either std/err or std/out</param>
/// <param name="exe">The executable to run, may be unqualified or contain environment variables</param>
/// <param name="args">The list of unescaped arguments to provide to the executable</param>
/// <returns>Returns process' exit code after the program exits</returns>
/// <exception cref="System.IO.FileNotFoundException">Raised when the exe was not found</exception>
/// <exception cref="System.ArgumentNullException">Raised when one of the arguments is null</exception>
/// <exception cref="System.ArgumentOutOfRangeException">Raised if an argument contains '\0', '\r', or '\n'</exception>
public static int Run(Action<string> output, string exe, params string[] args)
{
if (String.IsNullOrEmpty(exe))
throw new FileNotFoundException();
if (output == null)
throw new ArgumentNullException("output");
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.WorkingDirectory = Environment.CurrentDirectory;
psi.FileName = FindExePath(exe);
psi.Arguments = args[0];
using (Process process = Process.Start(psi))
using (ManualResetEvent mreOut = new ManualResetEvent(false),
mreErr = new ManualResetEvent(false))
{
process.OutputDataReceived += (o, e) => { if (e.Data == null) mreOut.Set(); else output(e.Data); };
process.BeginOutputReadLine();
process.ErrorDataReceived += (o, e) => { if (e.Data == null) mreErr.Set(); else output(e.Data); };
process.BeginErrorReadLine();
process.WaitForExit();
mreOut.WaitOne();
mreErr.WaitOne();
return process.ExitCode;
}
}
像魅力一样工作。 现在我想用这个函数执行一个vbscript。 我称之为:
int exit_code = ProcessUtility.Run(output_action, "cscript.exe", "//Nologo" + save_parameter_string);
这也运行良好,但问题是,它运行得很好。 如果我执行包含错误的vbscript,则退出代码也是&#34; 0&#34; ( 输出包含正确的&#34;异常&#34;从vbscript,在我的情况下: &#34; ... \ test.vbs(145,12)Microsoft VBScript中的运行时错误:对象必需&#34; 但退出代码为0。
有没有人有想法,为什么?
答案 0 :(得分:1)
或者,您可以使用VBScript -- Using error handling中描述的On Error Resume Next
技术。
简单地说,将原始脚本包装在这种类型的“异常处理程序”中:
On Error Resume Next
YourOriginalUnsafeCode()
' This would make cscript return non-zero in case of error'
if err.number <> 0 then WScript.quit err.number
Sub YourOriginalUnsafeCode()
' Your original code goes into this function'
a = 1 / 0
a = 2
End Sub
答案 1 :(得分:0)
我把它放在BAT文件中以生成运行时错误:
echo x = 1/0 > foo.vbs
cscript foo.vbs
echo %ERRORLEVEL%
输出:
C:\null\foo.vbs(1, 1) Microsoft VBScript runtime error: Division by zero
0
退出代码为0
,因此您无法使用退出代码检测运行时错误。
(%ERRORLEVEL%
仅1
语法错误(echo x = ?1/0 > foo.vbs
))
您需要解析输出或确定StdErr的行为,或者您可以通过ScriptControl从.Net运行VBScript,这会生成标准异常:How to execute VBScript command within textbox from C# application?