ShareX/ShareX.HelpersLib/CLI/ExternalCLIManager.cs

117 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
2014-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 ShareX Developers
2014-05-09 11:08:30 +12:00
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.Diagnostics;
using System.IO;
using System.Text;
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
private Process process = new Process();
2014-05-09 13:16:43 +12:00
public virtual int Open(string path, string args = null)
{
2014-05-09 18:41:16 +12:00
DebugHelper.WriteLine("CLI: \"{0}\" {1}", path, args);
2014-05-09 13:16:43 +12:00
if (File.Exists(path))
2014-05-09 03:17:42 +12:00
{
2014-05-09 13:16:43 +12:00
ProcessStartInfo psi = new ProcessStartInfo(path);
2014-05-09 03:17:42 +12:00
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 13:16:43 +12:00
psi.WorkingDirectory = Path.GetDirectoryName(path);
psi.StandardOutputEncoding = Encoding.UTF8;
psi.StandardErrorEncoding = Encoding.UTF8;
2014-05-09 01:28:46 +12:00
2014-05-09 11:08:30 +12:00
process.EnableRaisingEvents = true;
2014-06-04 23:44:37 +12:00
if (psi.RedirectStandardOutput) process.OutputDataReceived += cli_OutputDataReceived;
if (psi.RedirectStandardError) process.ErrorDataReceived += cli_ErrorDataReceived;
2014-05-09 11:08:30 +12:00
process.StartInfo = psi;
process.Start();
2014-06-04 23:44:37 +12:00
if (psi.RedirectStandardOutput) process.BeginOutputReadLine();
if (psi.RedirectStandardError) process.BeginErrorReadLine();
2014-05-09 11:08:30 +12:00
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)
{
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
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();
}
}
}
}