如何使用C#记录命令提示符下显示的echo消息?

时间:2015-07-22 12:07:02

标签: windows command-line batch-file c#

我使用下面的函数在C#中调用EAR_Encrypt_FTP.bat。有没有办法在运行批处理文件时跟踪cmd.exe中显示的echo消息?

我需要处理命令提示符屏幕上显示的回显消息记录过程。

private static short batchfileInvoke(string ftpFileName, string headerFile)
{
    short value = 0;
    Process p = null;
    try
    {
        p = new Process();
        ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.FileName = "cmd.exe";
        string argument1 = "EAR_Encrypt_FTP.bat";
        string test = "\"" + ftpFileName + "\"";
        string argument2 = test;
        string test1 = "\"" + headerFile + "\"";
        string argument3 = test1;
        p.StartInfo.WorkingDirectory = Path.GetDirectoryName(argument1);
        startInfo.Arguments = "/C EAREncryptFTP.bat " + argument2 + " " + argument3;
        p.StartInfo = startInfo;
        p.Start();
        p.WaitForExit();
        int value2 = p.ExitCode;
        value = (short)value2;
        if(value==0)
        {
            Console.WriteLine("Encryption process is done");
        }                
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception Occurred :{0},{1}",
                    ex.Message, ex.StackTrace.ToString());
        value = -4;
    }
    return value;
}  

0 个答案:

没有答案
相关问题