Process使用

     public static string RunCmd(string cmd)
       {
           cmd = cmd.Trim().TrimEnd('&') + "&exit";
           string output = "";
           using (Process p = new Process())
           {
               p.StartInfo.FileName = CmdPath;
               p.StartInfo.UseShellExecute = false;       
               p.StartInfo.RedirectStandardInput = true;  
               p.StartInfo.RedirectStandardOutput = true;  
               p.StartInfo.RedirectStandardError = true;  
               p.StartInfo.CreateNoWindow = true;         
               p.Start();

             
               p.StandardInput.WriteLine(cmd);
               p.StandardInput.AutoFlush = true;

             
               output = p.StandardOutput.ReadToEnd();
               p.WaitForExit();
               p.Close();
           }

           string txt = output;
           return txt;
       }
 #region cmd命令调用

        public void StartCmdEvent(string c, HashListItem item)
        {
            Process p = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.WorkingDirectory = Path.GetDirectoryName(ExePath);
            psi.FileName = ExePath;
            psi.Arguments = c;
            psi.CreateNoWindow = true;
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            p.OutputDataReceived += P_OutputDataReceived;
            p.EnableRaisingEvents = true;                      // 启用Exited事件  
            p.Exited += new EventHandler(CmdProcess_Exited);   // 注册进程失败事件  
            p.StartInfo = psi;
            item.WorkStatus = HashListItem.Status.Running;
            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
        }

//退出
private void CmdProcess_Exited(object sender, EventArgs e) { Process p = sender as Process; }

//输出 private void P_OutputDataReceived(object sender, DataReceivedEventArgs e) { Process p = sender as Process; } #endregion

ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName = hashcatFile;
                 
                    string hash = radioButton1.Checked ? textBox1.Text : HashfileTextbox.Text;
                    string ss = " -m " + hashType + " -a " + attackMode + " -i --increment-min " + BruteforceMinUpDown.Value.ToString() + " --increment-max " + BruteforceMaxUpDown.Value.ToString() + " -o " + hashcatLoc + "cracked/" + timestamp + ".cracked " + hash;
                    psi.Arguments = " -m " + hashType +" -a " + attackMode +" -i --increment-min " + BruteforceMinUpDown.Value.ToString() +" --increment-max " + BruteforceMaxUpDown.Value.ToString() +" -o " + hashcatLoc +"cracked/" + timestamp +".cracked " + hash;
                    p.StartInfo = psi;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardInput = true;
                    p.StartInfo.CreateNoWindow = true;
                    p.Start();
                    StreamWriter inputWriter = p.StandardInput;
                    Invoker.SetText(metroLabel6, "No speed data available...");
                    Invoker.SetText(metroLabel7, "");
                    while (!p.StandardOutput.EndOfStream)
                    {
                        string standard_output = p.StandardOutput.ReadLine();

                        if (standard_output.StartsWith("Speed"))
                        {
                            Invoker.SetText(metroLabel6,"Speed:" + Core.StringBetween(":"," (", standard_output));
                        }
                        else if (standard_output.StartsWith("Guess.Mask"))
                        {
                            Invoker.SetText(metroLabel7,"Cracking length:" + Core.StringBetween("[","]", standard_output));
                        }
                    }

                    p.WaitForExit();

原文地址:https://www.cnblogs.com/qc-id-01/p/8309411.html