ShareX/HelpersLib/CLI/ExternalCLIManager.cs

125 lines
3.8 KiB
C#
Raw Normal View History

2014-05-09 11:08:30 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2014 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
namespace HelpersLib
{
public abstract class ExternalCLIManager : IDisposable
{
2014-05-09 11:08:30 +12:00
public event DataReceivedEventHandler OutputDataReceived;
public event DataReceivedEventHandler ErrorDataReceived;
2014-05-09 11:08:30 +12:00
public StringBuilder Output { get; private set; }
public StringBuilder Errors { get; private set; }
2014-05-09 11:08:30 +12:00
private Process process = new Process();
2014-05-09 11:08:30 +12:00
public virtual int Open(string cliPath, string args = null)
{
2014-05-09 03:17:42 +12:00
if (File.Exists(cliPath))
{
2014-05-09 11:08:30 +12:00
Output = new StringBuilder();
Errors = new StringBuilder();
2014-05-09 10:18:14 +12:00
2014-05-09 03:17:42 +12:00
ProcessStartInfo psi = new ProcessStartInfo(cliPath);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
2014-05-09 11:08:30 +12:00
psi.RedirectStandardError = true;
2014-05-09 03:17:42 +12:00
psi.Arguments = args;
2014-05-09 11:08:30 +12:00
psi.WorkingDirectory = Path.GetDirectoryName(cliPath);
2014-05-09 01:28:46 +12:00
2014-05-09 11:08:30 +12:00
process.EnableRaisingEvents = true;
process.OutputDataReceived += cli_OutputDataReceived;
process.ErrorDataReceived += cli_ErrorDataReceived;
process.StartInfo = psi;
process.Start();
process.BeginErrorReadLine();
process.WaitForExit();
return process.ExitCode;
}
2014-05-09 03:17:42 +12:00
2014-05-09 11:08:30 +12:00
return -1;
}
2014-05-09 11:08:30 +12:00
private void cli_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
Output.AppendLine(e.Data);
2014-05-09 10:18:14 +12:00
2014-05-09 11:08:30 +12:00
if (OutputDataReceived != null)
{
OutputDataReceived(sender, e);
}
}
}
2014-05-09 11:08:30 +12:00
private void cli_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
2014-05-09 11:08:30 +12:00
if (e.Data != null)
{
2014-05-09 11:08:30 +12:00
Errors.AppendLine(e.Data);
if (ErrorDataReceived != null)
{
ErrorDataReceived(sender, e);
}
}
}
2014-05-09 11:08:30 +12:00
public void WriteInput(string input)
{
2014-05-09 11:08:30 +12:00
if (process != null && process.StartInfo != null && process.StartInfo.RedirectStandardInput)
{
process.StandardInput.WriteLine(input);
}
}
2014-05-09 11:08:30 +12:00
public virtual void Close()
{
2014-05-09 11:08:30 +12:00
if (process != null)
{
2014-05-09 11:08:30 +12:00
process.CloseMainWindow();
}
}
public void Dispose()
{
2014-05-09 11:08:30 +12:00
if (process != null)
{
2014-05-09 11:08:30 +12:00
process.Dispose();
}
}
}
}