从C#代码以批处理(.bat)文件查看输出 [英] View Output in a Batch (.bat) file from C# code

查看:324
本文介绍了从C#代码以批处理(.bat)文件查看输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#代码来执行批处理文件。我想在命令提示符下的bat文件中显示信息。
这是我新编辑的c#代码:

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

 }

}

以下是我的Demo1.bat文件:

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

推荐答案

使用 Process.OutputStream 或收听 Process.OutputDataReceived 事件。

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

示例:

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

这篇关于从C#代码以批处理(.bat)文件查看输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆