Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试使用C执行命令行脚本时获取System.InvalidOperation异常#_C#_.net_C# 4.0_System.diagnostics_Invalidoperationexception - Fatal编程技术网

C# 尝试使用C执行命令行脚本时获取System.InvalidOperation异常#

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

我正在尝试执行一个命令,使用生成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 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.
}