2016-07-28 1 views
0

cmd를 열고 명령을 실행하여 출력을 얻으려면 약간의 코드가 있지만 항상 두 번 실행되며 출력이 누락되는 경우가 있습니다. 여기 C# ProcessStartInfo 항상 중복 프로세스를 실행하십시오.

enter image description here

내 코드입니다, 나는 많은 시간을 체크 다시하지만 원인을 알아낼 수 없습니다.

using System; 
using System.Diagnostics; 
using System.IO; 
using System.Security.Cryptography; 
using System.Security.Permissions; 
using System.Text; 
using System.Threading; 


namespace CommandHandler 
{ 
    class Program 
    { 
     public static string change_file = AppDomain.CurrentDomain.BaseDirectory + @"change\change.txt"; 
     public static void Main() 
     { 
      //Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); 
      Console.BackgroundColor = ConsoleColor.Black; 
      Console.ForegroundColor = ConsoleColor.Green; 

      //ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c php d:/test.php") 
      ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c D:\\php64\\php D:\\xampp\\htdocs\\xxx\\bin\\listen.php") 
      { 
       WindowStyle = ProcessWindowStyle.Hidden, 
       UseShellExecute = false, 
       RedirectStandardOutput = true, 
       CreateNoWindow = true 
      }; 

      Process process = Process.Start(startInfo); 
      //process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived); 
      process.OutputDataReceived += new DataReceivedEventHandler((sender, e) => 
      { 
       // Prepend line numbers to each line of the output. 
       if (!String.IsNullOrEmpty(e.Data)) 
       { 
        String value = e.Data.ToLower(); 
        Console.WriteLine(e.Data); 
        if (value.Contains("php fatal error:")) 
        { 
         string hash = md5(DateTime.Now.ToString()); 
         System.IO.File.WriteAllText(change_file, hash); 
        } 
       } 
      }); 
      process.BeginOutputReadLine(); 
      process.Start(); 
      process.WaitForExit(); 
      Console.ReadKey(); 
     } 

     public static byte[] encryptData(string data) 
     { 
      System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider(); 
      byte[] hashedBytes; 
      System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
      hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data)); 
      return hashedBytes; 
     } 

     public static string md5(string data) 
     { 
      return BitConverter.ToString(encryptData(data)).Replace("-", "").ToLower(); 
     } 
    } 
} 

어떤 아이디어가 있습니까?

+1

어쩌면 그것은)입니까? – OrMiz

+1

솔직히, 내가 왜 투표를했는지 모르겠습니다. 타당한 이유가 없습니다. –

+0

@roryap 투표를하지 않거나 투표를하지 않겠습니다. 나는 단지 내 문제를 해결하기를 원했고, 나는 그들의 이력서에 Stackoverflow에서 점수를 얻는 많은 사람들을 알고 있으며, 내 의견으로는 정말 시간 낭비이다. 하지만 투표를 클릭하면 정말 즐겁습니다. – vietnguyen09

답변

5

Process.Start()을 두 번 호출하기 때문에

Process process = Process.Start(startInfo); 

다시 여기에, 생성자의 하단 : 여기 때 인스턴스 process를 만들

당신이 (Process.Start를에 두 번 호출하기 때문에
process.Start(); 
관련 문제