@MrCheatEugene
Человек. Учусь кодить.

Почему C# не может найти файл, если он существует?

Привет. Пишу программу на C# под Windows, которая запускает внешний RTSP-сервер.
При запуске следующего кода, процесс завершается с кодом -1 и с ошибкой "Не удаётся найти указанный файл", хотя при запуске процесса через командную строку/Win+R всё работает.

Код(class1.cs):
public static class ProcessAsyncHelper
{
    public static async Task<ProcessResult> ExecuteShellCommand(string command, string arguments, int timeout)
    {
        var result = new ProcessResult();

        using (var process = new Process())
        {
            // If you run bash-script on Linux it is possible that ExitCode can be 255.
            // To fix it you can try to add '#!/bin/bash' header to the script.

            process.StartInfo.FileName = command;
            process.StartInfo.Arguments = arguments;
            Console.WriteLine(process.StartInfo.Arguments);
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;

            var outputBuilder = new StringBuilder();
            var outputCloseEvent = new TaskCompletionSource<bool>();

            process.OutputDataReceived += (s, e) =>
                                            {
                                                // The output stream has been closed i.e. the process has terminated
                                                if (e.Data == null)
                                                {
                                                    outputCloseEvent.SetResult(true);
                                                }
                                                else
                                                {
                                                    outputBuilder.AppendLine(e.Data);
                                                }
                                            };

            var errorBuilder = new StringBuilder();
            var errorCloseEvent = new TaskCompletionSource<bool>();

            process.ErrorDataReceived += (s, e) =>
                                            {
                                                // The error stream has been closed i.e. the process has terminated
                                                if (e.Data == null)
                                                {
                                                    errorCloseEvent.SetResult(true);
                                                }
                                                else
                                                {
                                                    errorBuilder.AppendLine(e.Data);
                                                }
                                            };

            bool isStarted;

            try
            {
                isStarted = process.Start();
            }
            catch (Exception error)
            {
                // Usually it occurs when an executable file is not found or is not executable

                result.Completed = true;
                result.ExitCode = -1;
                result.Output = error.Message;

                isStarted = false;
            }

            if (isStarted)
            {
                // Reads the output stream first and then waits because deadlocks are possible
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                // Creates task to wait for process exit using timeout
                var waitForExit = WaitForExitAsync(process, timeout);

                // Create task to wait for process exit and closing all output streams
                var processTask = Task.WhenAll(waitForExit, outputCloseEvent.Task, errorCloseEvent.Task);

                // Waits process completion and then checks it was not completed by timeout
                if (await Task.WhenAny(Task.Delay(timeout), processTask) == processTask && waitForExit.Result)
                {
                    result.Completed = true;
                    result.ExitCode = process.ExitCode;
                    Console.WriteLine(process.ExitCode);
                    // Adds process output if it was completed with error
                    if (process.ExitCode != 0)
                    {
                        result.Output = outputBuilder.ToString();
                    }
                }
                else
                {
                    try
                    {
                        // Kill hung process
                        process.Kill();
                    }
                    catch
                    {
                    }
                }
            }
        }

        return result;
    }


    private static Task<bool> WaitForExitAsync(Process process, int timeout)
    {
            return Task.Run(() => process.WaitForExit(timeout));
    }


    public struct ProcessResult
    {
        public bool Completed;
        public int? ExitCode;
        public string Output;
    }
}


Form1.cs
public ProcessAsyncHelper.ProcessResult x;
        async private void button7_Click(object sender, EventArgs e)
        {
            x = await ProcessAsyncHelper.ExecuteShellCommand("cmd /c C:\\скачки\\rtsp-simple-server_v0.18.0_windows_amd64\\rtsp-simple-server.exe", "", 400);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Console.WriteLine(x.ExitCode.Value.ToString());
            Console.WriteLine(x.Completed);
            Console.WriteLine(x.Output);
            Console.WriteLine(x.ToString());
        }

Как можно решить эту проблему?
  • Вопрос задан
  • 314 просмотров
Пригласить эксперта
Ответы на вопрос 1
Adler_lug
@Adler_lug
Исходя из
ExecuteShellCommand(string command, string arguments, int timeout)

должно быть так:
ProcessAsyncHelper.ExecuteShellCommand("cmd.exe", "/c C:\\скачки\\rtsp-simple-server_v0.18.0_windows_amd64\\rtsp-simple-server.exe",  400);
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы