这是我使用的代码。它假设源代码与可执行文件位于同一文件夹中,并且如果需要,则包括运行 BibTeX(如果需要,只需排除第二个进程)。
string filename = "<your LaTeX source file>.tex";
Process p1 = new Process();
p1.StartInfo.FileName = "<your path to>\pdflatex.exe";
p1.StartInfo.Arguments = filename;
p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.UseShellExecute = false;
Process p2 = new Process();
p2.StartInfo.FileName = "<your path to>\bibtex.exe";
p2.StartInfo.Arguments = Path.GetFileNameWithoutExtension(filename);
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.UseShellExecute = false;
p1.Start();
var output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
p2.Start();
output = p2.StandardOutput.ReadToEnd();
p2.WaitForExit();
p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
是的,这可以稍微清理一下,当然,如果不调用 BibTeX,则可以在循环中调用(建议使用 3 次,以确保所有引用都正确)。此外,没有异常处理,因此您可能需要在进程调用等周围添加 try / catch 块。