简体   繁体   English

尝试重定向标准和/或错误时无输出。 我如何从中读取输出?

[英]No output when trying to redirect standard and/or error. How can i read the output from this?

When i try to redirect output from a executable running in batch mode there are only a few lines that shows up and there are no more output. 当我尝试从以批处理模式运行的可执行文件重定向输出时,只有几行显示,并且没有更多输出。 Although i have set the window to not be visible it pops up anyway, but there are no output in the window so clearly the output are redirected but not showing. 虽然我已将窗口设置为不可见,但无论如何都会弹出该窗口,但是窗口中没有输出,因此输出显然已重定向但未显示。

The executable is normally started like this: RustDedicated.exe -batchmode +more +arguments +here 可执行文件通常是这样启动的: RustDedicated.exe -batchmode +more +arguments +here

Now, when i try to spawn the exe file within my C# application only the very first lines outputs in debug console, the actual application window does not output anything. 现在,当我尝试在C#应用程序中生成exe文件时,调试控制台中仅输出第一行,实际的应用程序窗口将不输出任何内容。

Here are some code: 这是一些代码:

Process serverProcess;

private void StartServerThread()
{
    var serverArguments = GenerateServerArguments();
    var serverExecutable = Properties.Settings.Default.Rustserverexecutable;
    try
    {
        ProcessStartInfo info = new ProcessStartInfo
        {
            WorkingDirectory = System.IO.Path.GetDirectoryName(serverExecutable),
            FileName = serverExecutable,
            Arguments = "-batchmode",
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            //WindowStyle = ProcessWindowStyle.Hidden //Application window show no mather what.
        };
        serverProcess = new Process();
        serverProcess.StartInfo = info;
        serverProcess.OutputDataReceived += ServerProcess_OutputDataReceived;
        serverProcess.ErrorDataReceived += ServerProcess_ErrorDataReceived;
        serverProcess.Start();
        serverProcess.BeginOutputReadLine();
        serverProcess.BeginErrorReadLine();
        //serverProcess.StandardInput.WriteLine(serverExecutable + serverArguments); // I tested with cmd.exe and feeding it the path to the executable with arguments, same result.

    }
    catch (Exception e)
    {
        Console.WriteLine("Exception: " + e.Message);
    }
}

Also, as i have commented in the code i have tried to start CMD in interactive mode /K and the feeding it the path to the executable, and i have tried to redirect from a batch file wich does the same. 另外,正如我在代码中所做的评论一样,我尝试以交互方式/K启动CMD并将其提供给可执行文件的路径,并且尝试从批处理文件中重定向(至此)。 Same result. 结果相同。

I have tried both ErrorData and OutputData. 我已经尝试了ErrorData和OutputData。

Here are the code for that: 这是代码:

private void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
    if (!string.IsNullOrEmpty(e.Data.ToString()))
    {
        Application.Current.Dispatcher.BeginInvoke(new Action(() => {
            txt_ServerMessage.Document.Blocks.Add(new Paragraph(new Run("ErrorData: "+e.Data.ToString())));
        }));
    }
}

private void ServerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
    if (!string.IsNullOrEmpty(e.Data.ToString()))
    {
        Application.Current.Dispatcher.BeginInvoke(new Action(() => {
            txt_ServerMessage.Document.Blocks.Add(new Paragraph(new Run(e.Data.ToString())));
        }));
    }
}

The Game server is working, but no output to work with. 游戏服务器正在运行,但是没有可用的输出。

What can be the reason of this? 这可能是什么原因? How can i successfully redirect the output? 我如何成功重定向输出?

it's still actual for me. 对我来说仍然是现实。 Also work on automation work with Rust server, RustDedicated.exe. 也可以使用Rust服务器RustDedicated.exe进行自动化工作。 So I ended up with usage of RestDedicated.exe -logfile output.txt, and watching it. 因此,我最终使用了RestDedicated.exe -logfile output.txt,并进行了观察。

node.js TS example: node.js TS示例:

let pipe_file = 'output.txt';
const server_process = spawn('RustDedicated.exe', ['-batchmode', '-logfile', pipe_file],
{
    windowsHide: false,
    cwd: path.dirname(server_exe),
    shell: true,
    detached: true,
});
const pipe_full_path: string = path.join(path.dirname(server_exe), pipe_file);
let tail = new Tail(pipe_full_path);
fs.watchFile(pipe_full_path, (cur, prev) => {
    console.log(prev.mtime, cur.mtime);
});

tail.on("line", (data) => {
    console.log(data);
});

tail.on("error", (error) => {
    console.log('ERROR: ', error);
});    

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

相关问题 生成进程时如何重定向标准输出 - How to redirect standard output when spawning a process 从标准输出读取 - Read from standard output 如何在C#中访问PowerShell标准错误输出? - How can I access PowerShell standard error output in C#? 如何一次异步读取标准输出流和标准错误流 - How to asynchronously read the standard output stream and standard error stream at once 如何在C#中运行命令行应用程序并重定向/显示其标准输出,同时保留应用程序中的颜色? - How can I run a command line app in C# and redirect/display its standard output while preserving the colors from the app? 尝试读取标准 output 时进程挂起 - Process hangs while trying to read standard output XOR 字符串解密给我的 output 低于 0 错误。 我怎样才能绕过它? - XOR String Decryption give me as output below 0 Error. How can I bypass it? 我可以在C#中将流程的标准输出和标准错误与控制台窗口的输出合并吗? - Can I merge the standard output and standard error of a Process with the output of a console window in C#? 如何在C#、. net中仅异步重定向标准错误流而不是进程的标准输出流 - How to asynchronously redirect ONLY standard error stream and not standard output stream of a process in C#, .net 如何从Powershell重定向输出 - How Do I Redirect Output From Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM