One possible cause is include chinese characters,
//Wrong code
private void OpenClick(object sender, RoutedEventArgs e) { OpenFileDialog dialog=new OpenFileDialog(); dialog.Filter = "PDF Files|*.pdf|All Files|*.*"; if(dialog.ShowDialog()==true) { try { string pdfPath = dialog.FileName; if (File.Exists(pdfPath)) { webBrowser.Navigate(pdfPath); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
Solution
private void OpenClick(object sender, RoutedEventArgs e) { OpenFileDialog dialog=new OpenFileDialog(); dialog.Filter = "PDF Files|*.pdf|All Files|*.*"; if(dialog.ShowDialog()==true) { try { string pdfPath = dialog.FileName; if (File.Exists(pdfPath)) { Uri uri = new Uri(pdfPath, UriKind.RelativeOrAbsolute); webBrowser.Navigate(uri); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
The key located at convert string path as Uri
Uri uri = new Uri(pdfPath, UriKind.RelativeOrAbsolute); webBrowser.Navigate(uri);
标签:pdfPath,Files,sure,make,Uri,ex,dialog,address,new From: https://www.cnblogs.com/Fred1987/p/18298026