0

I have a bit of code to open cmd and run the command then get the output, but it's always running twice times, and sometimes output are missing.

enter image description here

Here is my code, I re-check many times but can't figure out what the cause.

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

Any idea?

3
  • 1
    Maybe it's because you call twice to process.Start()? Commented Jul 28, 2016 at 12:56
  • 1
    Honestly, I'm not sure why you got the down vote. Not for any valid reason, that is. Commented Jul 28, 2016 at 12:57
  • @roryap I don't care about down vote or up vote. I just wanted to solve my problem, I know a lot of people who take their score on Stackoverflow into their CV and in my opinion, it's really a waste of time. But I really enjoy when I click up vote for you. Commented Jul 28, 2016 at 13:31

1 Answer 1

6

Because you're calling Process.Start() twice. Here when you create the instance process:

Process process = Process.Start(startInfo); 

and again here, near the bottom of your constructor:

process.Start(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.