RedirectStandardOutput & OpenTextFile Conflict

S

ShieldsJared

Hello all,

So I recently found out about redirecting the Standard Output of a
vbscript back to my C# app but I've found a problem.

When I run a script that calls OpenTextFile, I do not receive anymore
of the standard output after that call is made.

Here is my code:
------------------------------------------------------------------------------------------------------
C#
------------------------------------------------------------------------------------------------------
public void Run(string path, string title)
{
//used to prevent output of the initial csript output
bool write = false;

string scriptName = path;
ProcessStartInfo si = new
ProcessStartInfo("cscript.exe", scriptName);
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.WindowStyle = ProcessWindowStyle.Hidden;
si.CreateNoWindow = true;

OutputBox.Clear();

Process p = Process.Start(si);

while (true)
{
string s = p.StandardOutput.ReadLine();

if (s == null
{
break;
}

//Allows me to skip past the initial headings from a
VBScript output via CSript.exe (this isn't the problem)
else if (s == "<<StartOutput>>")
{
write = true;
s = title + " started at " + DateTime.Now;
}

if(write)
OutputBox.AppendText(s);
}

return true;
}

---------------------------------------------------------------------------------------------------
VBScript
---------------------------------------------------------------------------------------------------

' Include Library Script
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
set oFile = fso.OpenTextFile("Tools.vbs",1)
sText = oFile.ReadAll
oFile.close
ExecuteGlobal sText

'Output
Wscript.Echo "<<StartOutput>>"
WScript.Echo "Yeah!"
Wscript.Echo "<<EndOutput>>"


---------------------------------------------------------------------------------------------------

Basically, If I comment out everything except for the CreateObject,
and the Echo's, the C# app picks up the Echo's just fine. As soon as
I uncomment the OpenTextFile.... The application only receives the
output regarding the initial startup of CScript.exe, but nothing
else..

Any ideas?

Thanks so much!
 
W

Willy Denoyette [MVP]

Hello all,

So I recently found out about redirecting the Standard Output of a
vbscript back to my C# app but I've found a problem.

When I run a script that calls OpenTextFile, I do not receive anymore
of the standard output after that call is made.

Here is my code:
------------------------------------------------------------------------------------------------------
C#
------------------------------------------------------------------------------------------------------
public void Run(string path, string title)
{
//used to prevent output of the initial csript output
bool write = false;

string scriptName = path;
ProcessStartInfo si = new
ProcessStartInfo("cscript.exe", scriptName);
si.RedirectStandardOutput = true;
si.RedirectStandardError = true;
si.UseShellExecute = false;
si.WindowStyle = ProcessWindowStyle.Hidden;
si.CreateNoWindow = true;

OutputBox.Clear();

Process p = Process.Start(si);

while (true)
{
string s = p.StandardOutput.ReadLine();

if (s == null
{
break;
}

//Allows me to skip past the initial headings from a
VBScript output via CSript.exe (this isn't the problem)
else if (s == "<<StartOutput>>")
{
write = true;
s = title + " started at " + DateTime.Now;
}

if(write)
OutputBox.AppendText(s);
}

return true;
}

---------------------------------------------------------------------------------------------------
VBScript
---------------------------------------------------------------------------------------------------

' Include Library Script
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
set oFile = fso.OpenTextFile("Tools.vbs",1)
sText = oFile.ReadAll
oFile.close
ExecuteGlobal sText

'Output
Wscript.Echo "<<StartOutput>>"
WScript.Echo "Yeah!"
Wscript.Echo "<<EndOutput>>"


---------------------------------------------------------------------------------------------------

Basically, If I comment out everything except for the CreateObject,
and the Echo's, the C# app picks up the Echo's just fine. As soon as
I uncomment the OpenTextFile.... The application only receives the
output regarding the initial startup of CScript.exe, but nothing
else..

Any ideas?

Thanks so much!


My guess is that the script host terminates execution because of an error.
try this:

On Error Resume Next
...

set oFile = fso.OpenTextFile("Tools.vbs",1)
if err.number <> 0 then
Wscript.Echo Err.Description
End If

Willy.
 
S

ShieldsJared

Willy, you are awesome. That completely worked! The wierd thing is,
the script ran fine when i ran the script on it's own. It only
recieved the error when I called it from my C# App. Oh well. I added
the full path to the file and now it works beautifully.

Thanks for your help!
 
Top