gpt4 book ai didi

c# - 尝试使用 C# 执行命令行脚本时出现 System.InvalidOperation 异常

转载 作者:行者123 更新时间:2023-11-30 18:19:24 26 4
gpt4 key购买 nike

我正在尝试使用 phantomjs 执行生成 PDF 文件的命令.

如果我使用命令提示符执行以下命令,一切正常。

C:\phantomjs-2.1.1\bin\phantomjs.exe C:\phantomjs-2.1.1\rasterize.js http://localhost:9992/index.html outputFile.pdf A4 landscape 0.1in

如果我尝试使用 C# 执行相同的操作,我会看到

System.InvalidOperation exception.

这是我使用的代码:

ProcessStartInfo startInfo = new ProcessStartInfo();
var url = "http://localhost:9992/index.html"
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false ;
startInfo.FileName = "C:\phantomjs-2.1.1\bin\phantomjs.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;

startInfo.Arguments = @"/c /K C:\phantomjs-2.1.1\rasterize.js " + url + "C:\temp\output.pdf A4 landscape 0.1in";

try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}

调试时请查看下图供inspector使用。

enter image description here

最佳答案

ProcessStartInfo startInfo = new ProcessStartInfo();
var url = "http://localhost:9992/index.html"
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false ; // This must be false to use env variable
startInfo.FileName = @"C:\phantomjs-2.1.1\bin\phantomjs.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;

startInfo.Arguments = @" C:\phantomjs-2.1.1\rasterize.js " + url + @" C:\phantomjs-2.1.1\output.pdf A4 landscape 0.1in";

try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}

关于c# - 尝试使用 C# 执行命令行脚本时出现 System.InvalidOperation 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38960449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com