【问题标题】:Trying to run same command in command prompt not working尝试在命令提示符下运行相同的命令不起作用
【发布时间】:2015-11-17 00:57:15
【问题描述】:

我正在制作一个程序,该程序在文件夹中查找受保护的 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 错误。有什么想法吗?

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

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

【问题讨论】:

  • 到目前为止您检查了什么?您是否跳过了代码并验证了正确的变量内容?
  • 什么是退出代码?你运行的那个exe有什么错误吗?如果您按照my answer here 中所示连接数据接收事件的事件,您可以捕获输出中的内容。
  • 您看到的错误是什么?您是否在 C# 程序的 PATH 中安装了 Ghostscript,因为您的 PATH 可能与命令行中的 PATH 不同。
  • @TobiasKnauss 我到目前为止检查的是我传入的变量参数。我的string arguments 变量如下"\"C:\\Users\\rwong\\Desktop\\RoundPoint\\1000965275\\1000965275_157_Credit File_10.PDF\" \"C:\\Users\\rwong\\Desktop\\RoundPoint\\1000965275\\1000965275_157_Credit File_10.png\""
  • 尝试不带参数启动进程以查看它是否正在运行。此外,为您的测试使用较短的路径和文件名,不带空格,例如 c:\temp\file.pdf。

标签: c# process imagemagick imagemagick-convert processstartinfo


【解决方案1】:

解决方案:

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

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

【讨论】:

    【解决方案2】:

    这将帮助您在 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);
        }
    }
    

    【讨论】:

    • 感谢您对这两个问题的输出!我在尝试调用DataReceivedEventHandler 时收到错误消息,称“非静态字段、方法或属性需要对象引用”,因此我在函数声明前添加了关键字 static,它使函数从未运行过。
    • 你在哪里添加了静态关键字。您在流程中也有一个错误处理程序。请查看您不应该在控制台应用程序中运行该程序。制作 WPF 或 WinForm 应用程序。
    猜你喜欢
    • 2019-01-02
    • 2012-11-15
    • 2018-06-20
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多