Run VBScript from C# with hidden window and capture output

1.8k views Asked by At

I am able to run VBScript from C# and capture standard output and error streams using this code:

var process = new Process();
process.StartInfo.FileName = "cscript";
process.StartInfo.WorkingDirectory = @"C:\scripts\";
process.StartInfo.Arguments = "//B //Nologo vbscript.vbs";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.Start();
...

This works fine but shows a console window. According to this answer, I can set process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden to hide the console window. I found that this only works if I don't capture the console output, i.e. if I comment out these lines:

//process.StartInfo.RedirectStandardOutput = true;
//process.StartInfo.RedirectStandardError = true;
//process.StartInfo.UseShellExecute = false;

Is there any way I can hide the console window and still be able to capture the output from the script?

1

There are 1 answers

1
SteveFerg On BEST ANSWER

you need to add 1 more line:

process.StartInfo.CreateNoWindow = true;