Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to execute a very simple test vbs file via .Net's System.Diagnostic.Process class and see the output from the file in my .Net app. My code won't run the vbs file, producing a Win32Exception: "The specified executable is not a valid Win32 application."
According to other sites I've looked at, this should be possible, so it's driving me crazy.My file has a single line:
Wscript.echo "Here's a message"My code to run it is below. Does anyone have anything that can help me?
private void RunScript()
{
Process scriptProcess = new Process();
StreamReader sr;
StreamReader err;scriptProcess.StartInfo.FileName = "test.vbs";
scriptProcess.StartInfo.WorkingDirectory = "E:\\Test\\TestScripts";
scriptProcess.StartInfo.RedirectStandardError = true;
scriptProcess.StartInfo.RedirectStandardOutput = true;
scriptProcess.StartInfo.UseShellExecute = false;try
{scriptProcess.Start();
sr = scriptProcess.StandardOutput;
err = scriptProcess.StandardError;scriptProcess.WaitForExit();
richTextBox1.Text += "OUTPUT: " + sr.ReadToEnd() + Environment.NewLine;
richTextBox2.Text += "ERROR: " + err.ReadToEnd() + Environment.NewLine;
}
catch (Exception ex)
{
String s = ex.Message;
}
}
}

That's because scripts are, by definition, not executable. You have two options:
- Set the FileName to cscript.exe, and the Arguments to E:\\Test\\TestScripts\\test.vbs.
- scriptProcess.StartInfo.UseShellExecute = true;Since you want to read its output, you don't really get a choice. You're going with the first one.

That's solved my problem - thanks very much, Razor 2.3, you're a champion. Solution 1 lets me run the script and capture anything it writes to standard output, which is exactly what I need.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |