【问题标题】:Process.Start not executing commandProcess.Start 不执行命令
【发布时间】:2015-08-21 18:24:31
【问题描述】:

我正在使用 ImageMagick C# 工具通过从 C# 调用可执行文件将 PDF 转换为 JPG。我相信我正确设置了命令,但它没有执行;它只是通过Process.Start(startInfo) 而不执行它。我确实看到弹出命令提示符,但没有任何反应。

string PNGPath = Path.ChangeExtension(Loan_list[f], ".png");
string PDFfile = '"' + Loan_list[f] + '"';
string PNGfile = '"' + PNGPath + '"';
string arguments = string.Format("{0} {1}", PDFfile, PNGfile);
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe");
startInfo.Arguments = arguments;
Process.Start(startInfo);

我不确定是不是因为我事先在每个参数中添加了双引号,但是在将其注释掉并再次运行它之后,它仍然跳过了。有什么想法吗?

编辑:为了增加清晰度,我期待 PDF 中的 JPG 文件,但我看不到这部分代码的输出文件。我在命令提示符下运行以下命令将 PDF 转换为 JPG

"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.PDF" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.png"

为了清楚起见,我在代码中明确调用了 convert.exe。该命令在命令提示符下工作正常,但是当将结构处理到 C# 时,它什么也不做。我看到代码进入其中,但它继续没有错误。

Edit2:根据要求,下面是进程退出代码的代码和输出

string PNGPath = Path.ChangeExtension(Loan_list[f], ".png");
string PDFfile = '"' + Loan_list[f] + '"';
string PNGfile = '"' + PNGPath + '"';
try
{
    Process myprocess = null;
    string[] arguments = { PDFfile, PNGfile };
    myprocess=Process.Start(@"C:\ProgramFiles\ImageMagick6.9.2Q16\convert.exe", String.Join(" ", arguments));
    Console.WriteLine("Process exit code: {0}", myprocess.ExitCode);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

进程退出代码:1

【问题讨论】:

  • 您是在您的用户上下文中、作为另一个用户还是在提升的上下文中运行程序?
  • 用户上下文和提升用户是什么意思?
  • 您是否“以管理员身份”运行它(或 Visual Studio)?
  • 尝试将您的进程更改为cmd.exe,并将参数更改为/c "C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.PDF" "C:\Users\rwong\Desktop\RoundPoint\1000965275\1000965275_157_Credit File_10.png"。这将运行一个新的命令窗口,该窗口将保持打开状态,因此您可以看到 ImageMagick 的输出
  • 希望same answer能帮到你。

标签: c# imagemagick-convert


【解决方案1】:

假设你是对的并且出现了问题(而不是进程刚刚执行得很快就退出了),你可以查看返回码如下:

if (Process.Start(startInfo) == null)
{
    int lastError = Marshal.GetLastWin32Error();
}

然后你去这里查找错误代码:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx

希望该供应商实际上在失败时设置了错误代码(他们可能会也可能不会)。

【讨论】:

  • 杰夫,感谢您的输入,但代码未进入该条件语句。我还编辑了帖子以更清楚地说明我的问题。
  • Process.Start 没有设置最后一个错误,所以调用 Marshal.GetLastWin32Error() 不会给你任何有用的东西。如果 Process.Start() 失败,它将抛出 Win32Exception。 Marshal.GetLastWin32Error() 只能用于 p/Invoke 调用。
  • 正确的是检查进程的存在代码是在Process.Start返回的Process对象上调用.ExitCode。
  • 是的,你是对的。我忘记了。但是 Win32Exception 将包含错误代码,对吧?
  • 会的。它有一个 NativeErrorCode 属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
相关资源
最近更新 更多