通过单击按钮以 Windows 形式打开 .pdf 文件 [英] Opening a .pdf file in windows form through a button click

查看:45
本文介绍了通过单击按钮以 Windows 形式打开 .pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On the current window that I have, I have a button. I want to be able to click the button and open up a .pdf file which is in the resources folder of this project. Is there an easy want to do this?

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

The other methods I've looked at uses filepaths but the filepaths may not be the same all the time but the .pdf file will be in the resources folder at all times. Is there a way to access this and open it when the button is clicked?

有什么类似的吗?

string filename = "instructions.pdf";
file.open();

问题解决了

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

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

With instructions.pdf in the bin/debug folder where the program.exe is.

推荐答案

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

To open a file with a system default viewer you need call

System.Diagnostics.Process.Start(filename);

但我不明白文件路径的问题.如果您需要从程序 .exe 文件到包含资源的文件夹的相对路径,那么您可以在文件路径中添加Resources\"或..\Resources\"(如果 Resources 文件夹更高).

But I haven't understood the problem with a filepath. If you need a relative path from the program .exe file to a folder with resources, then you can add "Resources\" or "..\Resources\" (if Resources folder is higher) to your filepath.

或者您可以将您的 pdf 作为嵌入资源添加到项目中,然后,当您需要打开它时,您可以使用

Or you can add your pdf to a project as an embedded resource and then, when you need to open it, you can save it to some temporal location using

Path.GetTempPath()

并打开它.

这篇关于通过单击按钮以 Windows 形式打开 .pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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