繁体   English   中英

无法在从 .NET Core 运行的 linux 命令中设置输入文件

[英]Not possible to set input file in linux command running from .NET Core

我在 .NET 容器上运行 .NET 核心应用程序 docker

当我从 linux 终端调用命令时,它运行良好:

./darknet detector test -out result.json < data/file-list.txt

但是当我从 .NET Core 启动进程时,我看到了错误。 流程运行器方法:

public static string RunCommand(string command, string args)
    {
        var process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = command,
                Arguments = args,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };

        process.Start();

        process.WaitForExit();

        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();
    return @$"{output}{Environment.NewLine}-------------------------------{Environment.NewLine}{error}";
}

调用代码:

string args = @$"detector test -out result.json < data/file-list.txt";
string output = ProcessRunner.RunCommand("./darknet", args);

这是 output 的一部分:

Cannot load image "<"
STB Reason: can't fopen

如何解决?

在启动进程时将RedirectStandartInput设置为 true 后,就可以编写进程的标准输入。 这是一个如何编写的示例:

        var process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "./ConsoleApp1.exe",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                RedirectStandardInput = true, // here you need
                UseShellExecute = false,
                CreateNoWindow = true,
            }
        };

        process.Start();
        using var file = File.OpenRead("./1.txt");
        using var reader = new StreamReader(file);
        while (true)
        {
            var line = await reader.ReadLineAsync();
            if (string.IsNullOrWhiteSpace(line)) break; // you can use some other stoping decision 
            await process.StandardInput.WriteLineAsync(line);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM