55 lines
2.2 KiB
C#
55 lines
2.2 KiB
C#
using System.Diagnostics;
|
|
using System.Text;
|
|
|
|
namespace File_Folder_Helper.Helpers;
|
|
|
|
internal static class HelperStart
|
|
{
|
|
|
|
internal static bool Start(string[] errorChecks, string[] successChecks, ProcessStartInfo processStartInfo, string errorFile)
|
|
{
|
|
bool result = false;
|
|
string standardError = string.Empty;
|
|
string standardOutput = string.Empty;
|
|
StringBuilder stringBuilder = new();
|
|
Process? process = Process.Start(processStartInfo);
|
|
long breakAfter = DateTime.Now.AddSeconds(20).Ticks;
|
|
for (short j = 0; j < short.MaxValue; j++)
|
|
{
|
|
if (process is null || process.HasExited || process.WaitForExit(500) || DateTime.Now.Ticks > breakAfter)
|
|
break;
|
|
}
|
|
if (process is not null && processStartInfo.RedirectStandardOutput)
|
|
{
|
|
_ = stringBuilder.Append(process.StandardOutput.ReadToEnd());
|
|
standardOutput = stringBuilder.ToString();
|
|
if (string.IsNullOrEmpty(standardOutput))
|
|
standardOutput = string.Concat(standardOutput);
|
|
if (!string.IsNullOrEmpty(standardOutput))
|
|
{
|
|
result = (from l in successChecks where standardOutput.Contains(l) select 1).Any();
|
|
if (result)
|
|
result = !(from l in errorChecks where standardOutput.Contains(l) select 1).Any();
|
|
}
|
|
}
|
|
if (process is not null && processStartInfo.RedirectStandardError)
|
|
{
|
|
standardError = process.StandardError.ReadToEnd();
|
|
if (!string.IsNullOrEmpty(standardError))
|
|
{
|
|
if (result)
|
|
{
|
|
result = (from l in successChecks where standardError.Contains(l) select 1).Any();
|
|
if (result)
|
|
result = !(from l in errorChecks where standardError.Contains(l) select 1).Any();
|
|
}
|
|
}
|
|
}
|
|
if (!result)
|
|
result = standardOutput == string.Empty && standardError == string.Empty;
|
|
if (!result)
|
|
File.WriteAllLines(errorFile, [standardOutput, Environment.NewLine, Environment.NewLine, standardError]);
|
|
return result;
|
|
}
|
|
|
|
} |