C# 尝试使用C执行命令行脚本时获取System.InvalidOperation异常#
我正在尝试执行一个命令,使用生成PDF文件 如果我使用命令提示符执行下面的命令,一切正常C# 尝试使用C执行命令行脚本时获取System.InvalidOperation异常#,c#,.net,c#-4.0,system.diagnostics,invalidoperationexception,C#,.net,C# 4.0,System.diagnostics,Invalidoperationexception,我正在尝试执行一个命令,使用生成PDF文件 如果我使用命令提示符执行下面的命令,一切正常 C:\phantomjs-2.1.1\bin\phantomjs.exe C:\phantomjs-2.1.1\rasterize.jshttp://localhost:9992/index.html outputFile.pdf A4横向0.1in 如果我尝试使用C#执行相同的操作,我会看到 System.InvalidOperation异常 以下是我正在使用的代码: ProcessStartInfo s
C:\phantomjs-2.1.1\bin\phantomjs.exe C:\phantomjs-2.1.1\rasterize.jshttp://localhost:9992/index.html outputFile.pdf A4横向0.1in
如果我尝试使用C#执行相同的操作,我会看到
System.InvalidOperation异常
以下是我正在使用的代码:
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.
}
调试时,有关检查器,请参见下图
为什么要启动
cmd.exe
为什么不设置startInfo.FileName=“C:\phantomjs-2.1.1\bin\phantomjs.exe”
然后执行startInfo.Arguments=@“C:\phantomjs-2.1.1\rasterize.js”+url+“output.pdf A4.0.1in”
您可能也想记录输出,以便查看发生了什么。查看其他一些process launcher实现,了解如何执行此操作,例如,如果要使用cmd.exe,请不要忘记在参数前面添加/C,否则cmd.exe将在不需要任何参数的情况下运行并立即关闭,你的代码说你的变量名为exeProcess
,但你的屏幕截图说它名为proc
,你确定你看到的是正确的对象吗?@ScottChamberlain:我尝试了很多事情,我想我改变了变量名&然后我拍了屏幕截图。
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.
}