Guten Tag,
ich habe kürzlich in einem anderem Forum Hilfestellung gegeben, um Befehle in den InputStream eines Prozesses zu schreiben. Rausgekommen ist die Klasse ProcessController, vieleicht kann ja jemand damit was anfangen.
ProcessController
AnwendungsBeispiel
ich habe kürzlich in einem anderem Forum Hilfestellung gegeben, um Befehle in den InputStream eines Prozesses zu schreiben. Rausgekommen ist die Klasse ProcessController, vieleicht kann ja jemand damit was anfangen.
C#-Quellcode
- class ProcessController : IDisposable
- {
- #region Events
- //***************
- public event EventHandler<ProcessControllerEventArgs.ProcessStart> OnProcessStart;
- protected virtual void EventProcessStart(Process p, DateTime startTime)
- {
- if (OnProcessStart != null)
- OnProcessStart(this, new ProcessControllerEventArgs.ProcessStart(p, startTime));
- }
- public event EventHandler<ProcessControllerEventArgs.ProcessStop> OnProcessStop;
- protected virtual void EventProcessStop(string processName, DateTime shutdownTime)
- {
- if (OnProcessStop != null)
- OnProcessStop(this, new ProcessControllerEventArgs.ProcessStop(processName, shutdownTime));
- }
- public event EventHandler<ProcessControllerEventArgs.ProcessOutputDataReceived> OnProcessOutputDataReceived;
- protected virtual void EventProcessOutputDataReceived(string data)
- {
- if (OnProcessOutputDataReceived != null)
- OnProcessOutputDataReceived(this, new ProcessControllerEventArgs.ProcessOutputDataReceived(data));
- }
- public event EventHandler<ProcessControllerEventArgs.ProcessErrorDataReceived> OnProcessErrorDataReceived;
- protected virtual void EventProcessErrorDataReceived(string data)
- {
- if (OnProcessErrorDataReceived != null)
- OnProcessErrorDataReceived(this, new ProcessControllerEventArgs.ProcessErrorDataReceived(data));
- }
- //=============
- #endregion Events
- #region Private Felder
- //***********************
- private Process process;
- private StreamWriter swCmdWriter;
- //==================
- #endregion Private Felder
- #region Konstruktoren
- //***********************
- /// <summary>
- /// ProcessController Konstruktor
- /// </summary>
- /// <param name="executableName">Name des Processes der gestartet werden soll</param>
- /// <param name="arguments">Startargumente die verwerndet werden sollen. Wenn keine Startargumente verwendet werden sollen, muss ein leerer string übergeben werden.</param>
- /// <param name="createNoWindow">Wenn False, wird keine Prozessfenster erstellt</param>
- public ProcessController(string executableName, string arguments, bool createNoWindow)
- {
- if (executableName == null || executableName == "")
- throw new ArgumentNullException("Executable name can´t be null");
- // Prozess Instanzieren
- process = new Process();
- process.StartInfo = CreateStartInfo(executableName, arguments, createNoWindow);
- process.OutputDataReceived += ProcessOutputDataReceived;
- process.ErrorDataReceived += ProcessErrorDataReceived;
- process.Start();
- // Event Feuern
- EventProcessStart(process, DateTime.Now);
- // Output des Prozesses Asynchron lesen
- process.BeginErrorReadLine();
- process.BeginOutputReadLine();
- // Standartinput des Prozess festlegen
- swCmdWriter = process.StandardInput;
- }
- //
- public ProcessController(string executableName, bool createNoWindow) : this(executableName, "", createNoWindow) { }
- //
- public ProcessController(string executableName, string arguments) : this(executableName, arguments, false) { }
- //
- public ProcessController(string executableName) : this(executableName, "") { }
- //==================
- #endregion Konstruktoren
- public void Dispose()
- {
- if (process != null) {
- process.OutputDataReceived -= ProcessOutputDataReceived;
- process.ErrorDataReceived -= ProcessErrorDataReceived;
- StopProcess();
- process.Dispose();
- process = null;
- }
- if (swCmdWriter != null)
- swCmdWriter.Dispose();
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// Stoppt den Prozess
- /// </summary>
- public void StopProcess()
- {
- if (process != null && IsProcessAlive) {
- process.Kill();
- // Event Feuern
- EventProcessStop(process.ProcessName, DateTime.Now);
- }
- }
- /// <summary>
- /// Schreibt den anbgegebenen Befehl in den Inputstream des Prozesses
- /// </summary>
- /// <param name="cmd">Befehl der in den Inputstream geschrieben werden soll</param>
- /// <returns>True wenn der befehl geschrieben wurde, andernfalls false</returns>
- public bool ExecuteCommand(string cmd)
- {
- if (process == null || !IsProcessAlive || cmd == "")
- return false;
- swCmdWriter.WriteLine(cmd);
- return true;
- }
- /// <summary>
- /// Erstellt ein ProcessStartInfo -Objekt
- /// </summary>
- /// <param name="executableName">Name des Processes der gestartet werden soll</param>
- /// <param name="arguments">Startargumente die verwerndet werden sollen.</param>
- /// <param name="createNoWindow">Wenn False, wird keine Prozessfenster erstellt</param>
- /// <returns>ProcessStartInfo</returns>
- private ProcessStartInfo CreateStartInfo(string executableName, string arguments, bool createNoWindow)
- {
- ProcessStartInfo procInfo = new ProcessStartInfo(executableName);
- procInfo.CreateNoWindow = createNoWindow;
- if (arguments != "")
- procInfo.Arguments = arguments;
- procInfo.UseShellExecute = false;
- procInfo.RedirectStandardInput = true;
- procInfo.RedirectStandardOutput = true;
- procInfo.RedirectStandardError = true;
- return procInfo;
- }
- /// <summary>
- /// Prüft ob eine Prozess Instanz aktiv ist
- /// </summary>
- private bool IsProcessAlive
- {
- get
- {
- if (process != null)
- return Process.GetProcessesByName(process.ProcessName).ToList().Find(x => x.Id == process.Id) != null;
- else
- return false;
- }
- }
- // Abonnierter Process -Events
- //********************************
- private void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
- {
- EventProcessOutputDataReceived(e.Data);
- }
- private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
- {
- EventProcessErrorDataReceived(e.Data);
- }
- }
C#-Quellcode
- class ProcessControllerEventArgs
- {
- public class ProcessStart : EventArgs
- {
- public Process CurrentProcess { get; private set; }
- public DateTime StartTime { get; private set; }
- public ProcessStart(Process p, DateTime startTime)
- {
- CurrentProcess = p;
- StartTime = startTime;
- }
- }
- public class ProcessStop : EventArgs
- {
- public string ProcessName { get; private set; }
- public DateTime ShutdownTime { get; private set; }
- public ProcessStop(string processName, DateTime shutdownTime)
- {
- ProcessName = processName;
- ShutdownTime = shutdownTime;
- }
- }
- public class ProcessOutputDataReceived : EventArgs
- {
- public string Data { get; private set; }
- public ProcessOutputDataReceived(string data)
- {
- Data = data;
- }
- }
- public class ProcessErrorDataReceived : EventArgs
- {
- public string Data { get; private set; }
- public ProcessErrorDataReceived(string data)
- {
- Data = data;
- }
- }
- }
C#-Quellcode
- ProcessController pc;
- #region GUI
- //************
- private void button_startProcess_Click(object sender, EventArgs e)
- {
- if (textBox_process.Text == "")
- return;
- if (pc != null) {
- pc.Dispose();
- pc = null;
- }
- pc = new ProcessController(textBox_process.Text, true);
- pc.OnProcessStart += pc_OnProcessStart;
- pc.OnProcessStop+=pc_OnProcessStop;
- pc.OnProcessOutputDataReceived +=pc_OnProcessOutputDataReceived;
- pc.OnProcessErrorDataReceived+=pc_OnProcessErrorDataReceived;
- }
- private void button_stopProcess_Click(object sender, EventArgs e)
- {
- pc.StopProcess();
- pc.Dispose();
- pc = null;
- }
- private void button_prozessCmd_Click(object sender, EventArgs e)
- {
- if (pc == null || textBox_cmd.Text == "")
- return;
- pc.ExecuteCommand(textBox_cmd.Text);
- }
- //==========
- #endregion GUI
- #region Abonnierte Events
- //***************************
- private void pc_OnProcessStart(object sender, ProcessControllerEventArgs.ProcessStart e)
- {
- WriteProcessMessage(string.Format("[{0}] Prozess {1} wurde gestartet...",e.StartTime.ToShortTimeString(), e.CurrentProcess.ProcessName));
- }
- private void pc_OnProcessStop(object sender, ProcessControllerEventArgs.ProcessStop e)
- {
- WriteProcessMessage(string.Format("[{0}] Prozess {1} wurde beendet...",e.ShutdownTime.ToShortTimeString(), e.ProcessName));
- }
- private void pc_OnProcessOutputDataReceived(object sender, ProcessControllerEventArgs.ProcessOutputDataReceived e)
- {
- WriteProcessMessage(e.Data);
- }
- private void pc_OnProcessErrorDataReceived(object sender, ProcessControllerEventArgs.ProcessErrorDataReceived e)
- {
- WriteProcessMessage(e.Data);
- }
- //=====================
- #endregion Abonnierte Events
- private void WriteProcessMessage(string msg)
- {
- if (textBox_output.InvokeRequired)
- textBox_output.Invoke(new MethodInvoker(delegate()
- {
- textBox_output.Text += msg + Environment.NewLine;
- }));
- else
- textBox_output.Text += msg + Environment.NewLine;
- }