当我尝试从以批处理模式运行的可执行文件重定向输出时,只显示几行,并且没有更多输出。虽然我已将窗口设置为不可见,但无论如何它都会弹出,但窗口中没有输出,所以输出被重定向但没有显示。
可执行文件通常如下所示:RustDedicated.exe -batchmode +more +arguments +here
现在,当我尝试在我的C#应用程序中生成exe文件时,只有调试控制台中的第一行输出,实际应用程序窗口不会输出任何内容。
以下是一些代码:
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);
}
}
另外,正如我在代码中评论的那样,我试图以交互模式/K
启动CMD并将其提供给可执行文件的路径,并且我尝试从批处理文件重定向。结果相同。
我已经尝试过ErrorData和OutputData。
以下是代码:
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())));
}));
}
}
游戏服务器正在运行,但无法使用输出。
这可能是什么原因?如何成功重定向输出?
答案 0 :(得分:0)
对我来说仍然是现实。 也可以使用Rust服务器RustDedicated.exe进行自动化工作。 因此,我最终使用了RestDedicated.exe -logfile output.txt并进行了观察。
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);
});