0

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 Answer 1

2

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());
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.