0

I have an c# code to execute the batch file. I want to show the information in the bat file in the command prompt. Here is my new edited c# code:

namespace CheckTime
{
class Program
{
    static void Main(string[] args)
    {
        Program Obj = new Program();

        int greetingId;
        int hourNow = System.DateTime.Now.Hour;

        if (hourNow < 12)
            greetingId = 0;
        else if (hourNow < 18)
            greetingId = 1;
        else
            greetingId = 2;
        System.Environment.ExitCode = greetingId;
        Obj.StartBatchFile(greetingId);


    }

   void StartBatchFile(int Gretting)
    {
        var p = new Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = string.Format(@"/C D:\Nimit Joshi\Visual Studio 2013\CheckTime\CheckTime\Demo1.bat {0}", Gretting);            
        p.OutputDataReceived += ConsumeData;


        try
        {
            p.Start();
            p.WaitForExit();
        }
        finally
        {
            p.OutputDataReceived -= ConsumeData;
        }
    }

    private void ConsumeData(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine(e.Data);
    }

 }

}

Following is my Demo1.bat file:

@echo off
:: Use %1 to get the first command line parameter
goto Greeting%1%

:Greeting
echo You have no entered a greeting.
goto end

:Greeting0
echo Good Morning
goto end

:Greeting1
echo Good Afternoon
goto end

:Greeting2
echo Good Evening
goto end

:end

It is always showing You have no entered a greeting

1
  • I is still showing you have no entered a greeting. But while debugging the Greeting value is 2, therefore it have to show "Good Evening" Commented Oct 28, 2013 at 14:07

2 Answers 2

5

Use the Process.OutputStream or listen to the Process.OutputDataReceived event.

Example:

private void ConsumeData(object sendingProcess, 
            DataReceivedEventArgs outLine)
{
    if(!string.IsNullOrWhiteSpace(outLine.Data))
        Console.WriteLine(outLine.Data);
}

p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.OutputDataReceived += ConsumeData;

try
{
    p.Start();
    p.BeginOutputReadLine();
    p.WaitForExit();
}
finally
{
    p.OutputDataReceived -= ConsumeData;
}

The batch file should be rewritten to not cause an infinite loop.

@echo off
:: Use %1 to get the first command line parameter
goto Greeting%1%

:Greeting
echo You have no entered a greeting.
goto end

:Greeting0
echo Good Morning
goto end

:Greeting1
echo Good Afternoon
goto end

:Greeting2
echo Good Evening
goto end

:end

C#

void StartBatchFile(int arg)
{
    var p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = string.Format(@"/C C:\temp\demo.bat {0}", arg);
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.OutputDataReceived += ConsumeData;

    try
    {
        p.Start();
        p.BeginOutputReadLine();
        p.WaitForExit();
    }
    finally
    {
        p.OutputDataReceived -= ConsumeData;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is it the full path to the file? e.g. c:\path\to\demo1.bat? The Process won't return any output if you're not executing the batch file.
sir my path is "D:\Nimit Joshi\Visual Studio 2013\CheckTime\CheckTime\Demo1.bat" as i provided in my code. I am running the c# code. The window is not showing the result.
If the path has spaces you need to quote it. e.g. @"/C ""D:\Nimit Joshi\Visual Studio 2013\CheckTime\CheckTime\Demo1.bat"" {0}"
0

You return (i.e. exit your program) before you call anotherMethod().

And that's good, otherwise you'd have an infinite loop of the .exe staring the .bat.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.