Код: Выделить всё
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
class ProcessRunner {
public static (string output, string error, int exitCode) Run(string exeName, string arguments) {
Process proc = new() {
StartInfo = new() {
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
FileName = exeName,
Arguments = arguments,
}
};
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
return (output, error, proc.ExitCode);
}
}
class Program
{
static void Main()
{
Console.WriteLine($"OS: {RuntimeInformation.OSDescription}");
Console.WriteLine($"Current Directory: {Directory.GetCurrentDirectory()}");
File.Delete("git");
try {
var (stdoutGit, stdErrGit, exitCodeGit) = ProcessRunner.Run("git", "status");
Console.WriteLine($"git (file missing) exit: '{exitCodeGit}', stdout '{stdoutGit}', stderr '{stdErrGit}'");
} catch (Exception ex) {
Console.WriteLine("Failed to run git (file missing attempt): " + ex.Message);
}
var (stdoutTouch, stdErrTouch, exitCodeTouch) = ProcessRunner.Run("touch", "git");
Console.WriteLine($"touch exit: '{exitCodeTouch}', stdout '{stdoutTouch}', stderr '{stdErrTouch}'");
try {
var (stdoutGit, stdErrGit, exitCodeGit) = ProcessRunner.Run("git", "status");
Console.WriteLine($"git (dummy file exists) exit: '{exitCodeGit}', stdout '{stdoutGit}', stderr '{stdErrGit}'");
} catch (Exception ex) {
Console.WriteLine("Failed to run git (dummy file exists) " + ex.Message);
}
}
}
< /code>
Вот выход: < /p>
OS: Ubuntu 24.04.1 LTS
Current Directory: /app
Failed to run git (file missing attempt): An error occurred trying to start process 'git' with working directory '/app'. No such file or directory
touch exit: '0', stdout '', stderr ''
Failed to run git (dummy file exists) An error occurred trying to start process '/app/git' with working directory '/app'. Permission denied
Подробнее здесь: https://stackoverflow.com/questions/796 ... cess-start