首页 > 解决方案 > Process.Start PDF 在文件夹中

问题描述

我无法让 Process.Start 简单地使用默认 PDF 查看器启动 PDF。

我尝试了许多 shell 执行、工作文件夹等的组合。不断给我“系统找不到指定的文件”或“目录名无效”

private void button1_Click(object sender, EventArgs e)
     {
        string filename = @"Milking and cooling software set 2018-39.pdf";
        MessageBox.Show(currentpath + @"\Astronaut A5 v1.5(b7)\documentation\" + filename);
        fullpath = currentpath + @"\Astronaut A5 v1.5(b7)\documentation";
        fullfile = fullpath + filename;
        ProcessStartInfo process = new ProcessStartInfo();
        process.WorkingDirectory = fullpath;
        process.UseShellExecute = false;
        process.FileName = fullfile;
        process.RedirectStandardOutput = true;
        process.Verb = "run as";
        Process.Start(process);
     }

为什么这么难,我已经尝试了几个小时来简单地启动 Acrobat Reader 来打开 PDF 文件。我可以在它的位置双击它没问题,但 C# 无法打开它,要么我收到 .NET 错误,要么 Adob​​e 打开并说它找不到文件。尝试了这么多“\””、完整路径、硬编码路径等的组合......令人难以置信,在这个时代,这很难编码。

标签: pdfdirectoryprocess.start

解决方案


您已告诉系统不要使用 ShellExecute。这意味着您提供的路径应该是一个实际的可执行程序。如果您想使用默认阅读器打开 PDF,请使用 ShellExecute。

process.UseShellExecute = true;

同样使用“run as”作为动词在这里没有任何意义,除非为 PDF 定义了这样一个动词,我很确定没有。那应该被删除。


推荐阅读