繁体   English   中英

如何从C#应用程序的批处理文件中读取消息

[英]How to read message from batch file from C# application

我有一个批处理文件,它将文件从一个文件夹复制到另一个文件夹。 因此,我想从C#Windows服务运行此文件,然后我想阅读脚本是否生成错误或脚本是否正确运行。 这是我午餐的代码,但是我不知道如何阅读脚本的消息:

脚本代码:

REM
REM This script moves files with results from GOLD server and saves them on MES06 server on .
REM IMPORT folder.
REM
REM Robocopy Options:
REM /R:2     Two retries on failed copies (default is 1 million)
REM /W:5     Wait 5 seconds between retries (default is 30 sec).
REM
REM GOLD QAS Inbound Folder: \\goldqas01.app.pmi\limscihome$\RootDirectory
REM 


for /f "delims=: tokens=2,3" %%j in (F:\MES2GOLD\copy_list_test.txt) do ROBOCOPY.EXE %%j %%j\..\BACKUP *.* /R:2 /W:5 /log+:%%j\..\LOGS\MES2GOLD.log & ROBOCOPY.EXE %%j %%k *.* /R:2 /W:5 /MOV /log+:%%j\..\LOGS\MES2GOLD.log

PAUSE

C#代码:

    public void execute(string workingDirectory, string command)
    {

        // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
        // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.

        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", @"/c C:\Users\mcastrio\Desktop\GOLD2MES.bat");

        procStartInfo.WorkingDirectory = workingDirectory;

        //This means that it will be redirected to the Process.StandardOutput StreamReader.
        procStartInfo.RedirectStandardOutput = true;
        //This means that it will be redirected to the Process.StandardError StreamReader. (same as StdOutput)
        procStartInfo.RedirectStandardError = true;

        procStartInfo.UseShellExecute = false;
        // Do not create the black window.
        procStartInfo.CreateNoWindow = true;
        // Now we create a process, assign its ProcessStartInfo and start it
        System.Diagnostics.Process proc = new System.Diagnostics.Process();

        //This is importend, else some Events will not fire!
        proc.EnableRaisingEvents = true;

        // passing the Startinfo to the process
        proc.StartInfo = procStartInfo;

        // The given Funktion will be raised if the Process wants to print an output to consol                    
        proc.OutputDataReceived += DoSomething;
        // Std Error
        proc.ErrorDataReceived += DoSomethingHorrible;
        // If Batch File is finished this Event will be raised
        proc.Exited += Exited;
    }

我们可以帮我吗?

您可以在循环内使用以下代码:

var startInfo = p.StartInfo;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.StandardOutputEncoding = Encoding.GetEncoding("ibm850");
startInfo.RedirectStandardOutput = true;
startInfo.FileName = filebatch;
startInfo.Arguments = arguments;

p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

或仅使用TestSharp库中的ProcessHelper.Run

ProcessHelper.Run(string exePath, string arguments = "", bool waitForExit = true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM