有关将值从VBScript返回到.NET的问题 [英] Question on returning values from VBScript to .NET
问题描述
我正在尝试建立一个能够从.NET运行VBScript文件的应用程序(请参阅此处),并且大多数设置都很好,但是我想对此进行测试,因此我需要能够从我的VBScripts返回数据。我发现我可以使用WScript.Quit([ErrorCode])来获取整数值,但是返回字符串呢?是否可以将它们提供给DataReceivedEventHandler?还是我需要看看其他方法?谢谢。
i'm trying to set up an application capable of running VBScript files from .NET (See here), and have most of it set up fine, but I want to test this out, so I need to be able to return data from my VBScripts. I've found that I can use WScript.Quit([ErrorCode]) to get back an integer value, but what about returning strings? Is it possible to feed them out to the DataReceivedEventHandler? Or do I need to look at a different method? Thanks.
推荐答案
您可以写入标准输出(它将输出重定向到事件处理程序)。我相信在VBScript中这是WScript.Stdout。
You can write to the standard output (which will redirect it to the event handler). I believe in VBScript this is WScript.Stdout.
如果您写了多行,则可以考虑使用StringWriter之类的东西来捕获它们,即
If you have multiple lines written out you may consider using something like a StringWriter to capture them all, ie
var p = new Process()
{
StartInfo = new ProcessStartInfo("netstat")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
}
};
var outputWriter = new StringWriter();
p.OutputDataReceived += (sender, args) => outputWriter.WriteLine(args.Data);
var errorWriter = new StringWriter();
p.ErrorDataReceived += (sender, args) => errorWriter.WriteLine(args.Data);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
if (p.ExitCode == 0)
{
Console.WriteLine(outputWriter.GetStringBuilder().ToString());
}
else
{
Console.WriteLine("Process failed with error code {0}\nMessage Was:\n{1}", p.ExitCode
, errorWriter.GetStringBuilder().ToString());
}
这篇关于有关将值从VBScript返回到.NET的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!