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

Trying to run same command in command prompt not working

我正在制作一个程序,用于在文件夹中查找受保护的 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);
            }
      }
}

我在命令提示符中有 运行 原始命令并且它有效,所以命令不是问题。下面的示例命令

"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。有什么想法吗?

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

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

这将帮助您 运行 在 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("[=10=]", string.Empty);
        //Show s 
        Console.WriteLine(s);
    }
}

解决方案:

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();

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