尝试在命令提示符下运行相同的命令不起作用

我正在制作一个程序,在文件夹中搜索安全的PDF并使用ImageMagick将它们转换为PNG文件。 以下是我的代码。

string WorkDir = @"C:\Users\rwong\Desktop\TestFiles"; Directory.SetCurrentDirectory(WorkDir); String[] SubWorkDir = Directory.GetDirectories(WorkDir); foreach (string subdir in SubWorkDir) { string[] filelist = Directory.GetFiles(subdir); for(int f = 0; f < filelist.Length; f++) { if (filelist[f].ToLower().EndsWith(".pdf") || filelist[f].EndsWith(".PDF")) { PDFReader reader = new Pdfreader(filelist[f]); bool PDFCheck = reader.IsOpenedWithFullPermissions; reader.CLose(); if(PDFCheck) { //do nothing } else { string PNGPath = Path.ChangeExtension(filelistf], ".png"); string PDFfile = '"' + filelist[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); } } } 

我已经在命令提示符下运行了raw命令,但它运行正常,命令不是问题。 下面的示例命令

 "C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" "C:\Users\rwong\Desktop\TestFiles\Test_File File_10.PDF" "C:\Users\rwong\Desktop\TestFiles\Test_File File_10.png" 

我环顾四周,并且有一些提示,我的变量中的空格可能会导致问题,但大多数线程都在讨论对参数名称进行硬编码,而他们只讨论1个参数。 我认为为每个变量添加双引号可以解决问题,但事实并非如此。 我还读到使用ProcessStartInfo会有所帮助但是没有骰子。 我猜这是我格式化2个参数以及如何调用命令,或者我使用ProcessStartInto错误的方式。 有什么想法吗?

编辑:根据下面的评论,我通过等待命令窗口退出进行额外的测试测试,我发现以下错误。

调用convert.exe时出错

旁注:我还不想使用GhostScript因为我觉得我非常接近使用ImageMagick的答案。

解:

 string PNGPath = Path.ChangeExtension(Loan_list[f], ".png"); string PDFfile = PNGPath.Replace("png", "pdf"); string PNGfile = PNGPath; Process process = new Process(); process.StartInfo.FileName = @"C:\Program Files\ImageMagick-6.9.2 Q16\convert.exe"; process.StartInfo.Arguments = "\"" + PDFfile + "\"" +" \"" + PNGPath +"\""; // Note the /c command (*) process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.Start(); //* Read the output (or the error) string output = process.StandardOutput.ReadToEnd(); Console.WriteLine(output); string err = process.StandardError.ReadToEnd(); Console.WriteLine(err); process.WaitForExit(); 

它不喜欢我格式化参数字符串的方式。

这可以帮助您在c#中运行命令,也可以在C#中获取控制台的结果。

 string WorkDir = @"C:\Users\rwong\Desktop\TestFiles"; Directory.SetCurrentDirectory(WorkDir); String[] SubWorkDir = Directory.GetDirectories(WorkDir); foreach (string subdir in SubWorkDir) { string[] filelist = Directory.GetFiles(subdir); for(int f = 0; f < filelist.Length; f++) { if (filelist[f].ToLower().EndsWith(".pdf") || filelist[f].EndsWith(".PDF")) { PDFReader reader = new Pdfreader(filelist[f]); bool PDFCheck = reader.IsOpenedWithFullPermissions; reader.CLose()l if(!PDFCheck) { string PNGPath = Path.ChangeExtension(filelistf], ".png"); string PDFfile = '"' + filelist[f] + '"'; string PNGfile = '"' + PNGPath + '"'; string arguments = string.Format("{0} {1}", PDFfile, PNGfile); Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.EnableRaisingEvents = true; p.StartInfo.CreateNoWindow = true; p.startInfo.FileName = "C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe"; p.startInfo.Arguments = arguments; p.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived); //You can receive the output provided by the Command prompt in Process_OutputDataReceived p.Start(); } } } private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (e.Data != null) { string s = e.Data.ToString(); s = s.Replace("\0", string.Empty); //Show s Console.WriteLine(s); } }