Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a .vbs file which should be accessed in c# Application to pass in the parameters and display the out.
Currently, when I execute the .vbs file through command prompt, I am being able to see the output.Whereas, the same when accessed through C# Diagostics's process. Am not getting how to display output.
Please help me to display output.

Please provide me an example.

What I have tried:

C# code:
C#
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    
    private static int lineCount=0;
  private static StringBuilder output= new StringBuilder();
    [STAThread]
    static void Main()
    {
        Process scriptProc = new Process();
        scriptProc.StartInfo.FileName = @"cscript";
        //scriptProc.StartInfo.WorkingDirectory = @"D:\vbs\"; //<---very important 
        scriptProc.StartInfo.Arguments = "//B //Nologo D:\vbs\test.vbs";
        scriptProc.StartInfo.UseShellExecute = true;
        scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
        scriptProc.StartInfo.UseShellExecute = false;
        scriptProc.StartInfo.RedirectStandardOutput = true;
        scriptProc.OutputDataReceived += new DataReceivedEventHandler((sender, e) =&gt;
        {
        if (!String.IsNullOrEmpty(e.Data))
        {
          lineCount++;
         output.Append("\n[" + lineCount + "]:" + e.Data);
         }
        });
        //try
        //{
            scriptProc.Start();
            scriptProc.BeginOutputReadLine();
            scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit
            Console.WriteLine(output);
            scriptProc.WaitForExit();
            scriptProc.Close();           //System.Diagnostics.Process.Start(@"cscript //B //Nologo D:\vbs\test.vbs");
        //}
        //catch(Exception exception)
        //{
        //    Console.WriteLine("Exception :" + exception.Message);

        //}
        Console.ReadLine();
    }
}

.vbsfile:
VB
intHighNumber = 100
intLowNumber = 1
For i = 1 to 5
    Randomize
    intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
    Wscript.Echo intNumber
Next
Posted
Updated 8-Aug-16 21:48pm
v2

1 solution

See ProcessStartInfo.RedirectStandardOutput Property (System.Diagnostics)[^]. It would also help if you explained exactly what happens when you run this code.
 
Share this answer
 
Comments
Member 11704877 9-Aug-16 8:13am    
When run the above code, nothing but the blank screen appears.
Richard MacCutchan 9-Aug-16 11:12am    
You will need to use your debugger to find out what output is being generated and why you are not displaying it.
Richard MacCutchan 9-Aug-16 11:14am    
I just ran your script and it did not produce any output. Maybe a little more testing is necessary.

Strange, it seems to work now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900