简体   繁体   English

wpf单击按钮在Adobe Reader中打开PDF文件

[英]wpf open PDF file in Adobe Reader with a click on a button

I need a button to open a PDF file with Adobe Reader. 我需要一个按钮来使用Adobe Reader打开PDF文件。 I have the following code but it does not work. 我有以下代码,但无法正常工作。 The file is inside Books folder on my application. 该文件在我的应用程序的Books文件夹内。

private void openPDF(object sender, RoutedEventArgs e)
{
    try
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        bookPDF = "/Books/" + dataRow.ItemArray[6].ToString();
        Uri pdf = new Uri(bookPDF, UriKind.Relative);
        process.StartInfo.FileName = new Uri(bookPDF, UriKind.Relative).ToString();
        process.Start();
        process.WaitForExit();
    }
    catch
    {
        MessageBox.Show("Could not open the file.", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
    }
}

I tested a simple string with a test pdf file to open and it opens. 我测试了一个简单的字符串,并打开了一个测试pdf文件。

...
String file = "C:\\pdf\\Windows_Server_2008_R2_Unleashed.pdf";
process.StartInfo.FileName = file;
process.Start();
process.WaitForExit();

Change your 6th line: 更改您的第六行:

bookPDF = "/Books/" + dataRow.ItemArray[6].ToString();

to: 至:

bookPDF = "Books\\" + dataRow.ItemArray[6].ToString();

Microsoft Windows naming convention uses backslashes as separator within paths https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx Microsoft Windows命名约定在路径https://msdn.microsoft.com/zh-cn/library/windows/desktop/aa365247%28v=vs.85%29.aspx中使用反斜杠作为分隔符

You can change your 6th line to : 您可以将第六行更改为:

bookPDF = string.Format(@"books\{0}", dataRow.ItemArray[6].ToString());         

And this should work if you have books folder in your application directory which has specified pdf. 如果您的应用程序目录中有指定PDF的books文件夹,则此方法应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM