namespace VideoBrowser.Helpers
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using VideoBrowser.Common;
///
/// Defines the
///
public static class ProcessHelper
{
#region Methods
///
/// Creates a Process with the given arguments, then returns it.
///
/// The fileName
/// The arguments
/// The output
/// The error
/// The environmentVariables
/// The
public static Process StartProcess(string fileName,
string arguments,
Action output,
Action error,
Dictionary environmentVariables)
{
var psi = new ProcessStartInfo(fileName, arguments)
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = AppEnvironment.GetLogsDirectory()
};
if (environmentVariables != null)
{
foreach (KeyValuePair pair in environmentVariables)
psi.EnvironmentVariables.Add(pair.Key, pair.Value);
}
var process = new Process()
{
EnableRaisingEvents = true,
StartInfo = psi
};
process.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrEmpty(e.Data))
{
return;
}
Logger.Info(e.Data);
output?.Invoke(process, e.Data);
};
process.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrEmpty(e.Data))
{
return;
}
Logger.Info(e.Data);
error?.Invoke(process, e.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
return process;
}
#endregion Methods
}
}