【问题标题】:programmatically opening adobe but pdf won't load以编程方式打开 adobe 但无法加载 pdf
【发布时间】:2025-12-24 19:35:12
【问题描述】:

所以我有代码以编程方式打开 pdf。代码可以很好地打开 adobe reader,但我会弹出一个对话框,提示该文件不存在。问题是我可以浏览到用于尝试在 Windows 浏览器中打开 pdf 的确切路径,此外还有一个 if 语句来判断文件是否存在。那么为什么adobe不打开pdf呢?

proc.StartInfo.FileName 上的 Adob​​e .exe 路径正确。

我找到了这个链接:https://visibleprocrastinations.wordpress.com/2009/08/20/there-was-an-error-opening-this-document-file-cannot-be-found-acrobat-reader/,但我不知道它是否仍然适用

PDF文件路径:

C:\Users\Printer\SharePoint\Partners - Doc\McG\Labels\TR109897\eLabels_TR109897.pdf

这是我正在使用的代码:

Process proc = new Process();

FileInfo file = new FileInfo(filepath);

if (file.Exists)
{
    //Define Location of adobe reader/command line
    proc.StartInfo.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    proc.StartInfo.Arguments = string.Format(@"{0}", file.FullName);
    proc.StartInfo.UseShellExecute = true;
    proc.StartInfo.CreateNoWindow = true;

    proc.Start();

    if (proc.HasExited == false)
        proc.WaitForExit(10000);

    proc.Close();
    return true;
}

【问题讨论】:

  • 首先,Process 只在本地机器上工作。如果您在 Web 应用程序中拥有它,那么该过程将发生在服务器上,而不是用户的 PC 上。考虑将“临时”pdf文件保存到用户临时文件夹并从那里打开它。您将不得不研究流媒体。
  • 它在本地机器上。
  • 不是 WPF 特定的,删除了标签。

标签: c# pdf


【解决方案1】:

听起来 shell 正在以一种意想不到的方式解析您的文件名。尝试将您的文件名用引号括起来:

proc.StartInfo.Arguments = string.Format("\"{0}\"", file.FullName);

【讨论】:

  • 我知道了:There was an error opening this document. The filename, directory name, or volume label syntax is incorrect.
  • 你没有把@ 放在那里,是吗?这会打乱编译器的解析。如果您坚持使用逐字字符串,则可以改用:proc.StartInfo.Arguments = string.Format(@"""{0}""", file.FullName);
  • 我刚刚注意到我做到了,给我几分钟来修复它,然后再试一次。