通过单击按钮以Windows窗体打开.pdf文件

Jed*_*931 8 c# pdf winforms

在我当前的窗口上,我有一个按钮.我希望能够单击该按钮并打开该项目的资源文件夹中的.pdf文件.有没有想要做到这一点?

我看过的其他方法使用文件路径,但文件路径可能不是一直相同,但.pdf文件始终位于资源文件夹中.有没有办法访问它并在单击按钮时打开它?

有什么事吗?

string filename = "instructions.pdf";
file.open();
Run Code Online (Sandbox Code Playgroud)

问题解决了

private void Button1_Click(object sender, EventArgs e)
{
    string filename = "instructions.pdf";
    System.Diagnostics.Process.Start(filename);
}
Run Code Online (Sandbox Code Playgroud)

使用program.exe所在的bin/debug文件夹中的instructions.pdf.

Art*_*tem 17

要使用系统默认查看器打开文件,您需要调用

System.Diagnostics.Process.Start(filename);
Run Code Online (Sandbox Code Playgroud)

但我还没有理解文件路径的问题.如果您需要从程序.exe文件到具有资源的文件夹的相对路径,则可以将"Resources \"或"..\Resources \"(如果Resources文件夹更高)添加到文件路径中.

或者您可以将pdf作为嵌入式资源添加到项目中,然后,当您需要打开它时,可以使用它将其保存到某个临时位置

Path.GetTempPath()
Run Code Online (Sandbox Code Playgroud)

并打开它.

  • Process.Start() 大部分工作。我看到错误:“指定的可执行文件不是此操作系统平台的有效应用程序”,该帖子已解决:/sf/answers/3294634241/ 我正在作为 .NET Core 3.1 WinForms add 运行在 Windows 10 下。 (5认同)