Exemplo n.º 1
0
        public bool TryLocateExecutable(string program, out string?path)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                if (Environment.GetEnvironmentVariable("PATH") is { } pathValue)
                {
                    string[] paths = pathValue.Split(';');
                    foreach (var basePath in paths)
                    {
                        string candidatePath = Path.Combine(basePath, program);
                        if (FileExists(candidatePath))
                        {
                            path = candidatePath;
                            return(true);
                        }
                    }
                }
            }
            else
            {
                using var which = ProcessHelper.CreateProcess("/usr/bin/which", program);
                which.Start();
                which.WaitForExit();

                switch (which.ExitCode)
                {
                case 0:     // found
                    string   stdout  = which.StandardOutput.ReadToEnd();
                    string[] results = stdout.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    path = results.First();
                    return(true);

                case 1:     // not found
                    break;

                default:
                    throw new Exception(
                              $"Unknown error locating '{program}' using /usr/bin/which. Exit code: {which.ExitCode}.");
                }
            }

            path = null;
            return(false);
        }
Exemplo n.º 2
0
 public Process CreateProcess(string args) => ProcessHelper.CreateProcess(_path, args);