在我当前的窗口中,我有一个按钮。我希望能够单击该按钮并打开该项目的资源文件夹中的.pdf文件。有没有想要做到这一点?
我看过的其他方法使用的是文件路径,但文件路径可能不会一直相同,但.pdf文件始终位于资源文件夹中。有没有办法访问它并在单击按钮时打开它?
一切都是什么?
string filename = "instructions.pdf";
file.open();
问题已解决
private void Button1_Click(object sender, EventArgs e)
{
string filename = "instructions.pdf";
System.Diagnostics.Process.Start(filename);
}
使用program.exe所在的bin / debug文件夹中的instructions.pdf。
答案 0 :(得分:13)
要使用系统默认查看器打开文件,您需要调用
System.Diagnostics.Process.Start(filename);
但我还没有理解文件路径的问题。 如果需要从程序.exe文件到具有资源的文件夹的相对路径,则可以将“Resources \”或“.. \ Resources \”(如果Resources文件夹更高)添加到文件路径中。
或者您可以将pdf作为嵌入式资源添加到项目中,然后,当您需要打开它时,可以使用
将其保存到某个临时位置Path.GetTempPath()
然后打开它。
答案 1 :(得分:0)
如果您想使用Adobe Reader或类似应用程序打开pdf文件,可以使用Process.Start函数。
ProcessStartInfo startInfo = new ProcessStartInfo("pathtofile");
Process.Start(startInfo);
这将与您在Windows文件夹中单击该文件时的行为相同。如果无法放置文件路径,则可以将文件从资源复制到临时文件夹并使用该路径。