尝试在命令提示符下运行相同的命令不起作用 [英] Trying to run same command in command prompt not working

查看:174
本文介绍了尝试在命令提示符下运行相同的命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a program that seeks out secured PDFs in a folder and converting them to PNG files using ImageMagick. Below is my code.

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命令在命令提示符下,它工作,所以命令不是问题。下面的示例命令

I have ran the raw command in command prompt and it worked so the command isn't the issue. Sample command below

"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错误的方式。有什么想法吗?

I looked around SO and there has been hints that spaces in my variable can cause an issue, but most of those threads talk about hardcoding the argument names and they only talk about 1 argument. I thought adding double quotes to each variable would solve the issue but it didn't. I also read that using ProcessStartInfo would have helped but again, no dice. I'm going to guess it is the way I formatted the 2 arguments and how I call the command, or I am using ProcessStartInto wrong. Any thoughts?

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

Based on the comments below I did the extra testing testing by waiting for the command window to exit and I found the following error.

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

Side note: I wouldn't want to use GhostScript just yet because I feel like I am really close to an answer using 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();

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

It didn't like the way I was formatting the argument string.

这篇关于尝试在命令提示符下运行相同的命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆