views:

43

answers:

1

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.

+1  A: 

You can write to the standard output (which will redirect it to the event handler). I believe in VBScript this is WScript.Stdout.

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());
        }
AlexCuse