Computing.Net > Forums > Programming > Use .Net Process class to run vbs f

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Use .Net Process class to run vbs f

Reply to Message Icon

Name: TheMightyKumquat
Date: July 31, 2008 at 23:42:14 Pacific
OS: Windows server 2003
CPU/Ram: Intel, igb
Product: Ipex T400
Comment:

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;
}
}
}



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: August 1, 2008 at 02:14:37 Pacific
Reply:

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.


0

Response Number 2
Name: TheMightyKumquat
Date: August 3, 2008 at 16:57:28 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Use .Net Process class to run vbs f

Run vb.net application in *.bat www.computing.net/answers/programming/run-vbnet-application-in-bat/15129.html

WZZIP zipping/VB.NET process class www.computing.net/answers/programming/wzzip-zippingvbnet-process-class/4063.html

Fail to Setup VB.NET www.computing.net/answers/programming/fail-to-setup-vbnet-/12179.html