1 using Microsoft.Win32; 2 3 namespace WpfApplication1 4 { 5 class WPFOpenFileDialog 6 { 7 public void OpenFile() 8 { 9 OpenFileDialog openFileDialog = new OpenFileDialog(); 10 openFileDialog.InitialDirectory = "D:"; // 初始化地址 11 openFileDialog.Filter = "文本文件|*.txt|PNG|*.png;*.jpg;*.bmp|视频|*.avi|全部|*.*"; //打开文件的格式(每两个“|”为一个单位) 12 if (openFileDialog.ShowDialog() == true) 13 { 14 string path = openFileDialog.FileName;// 打开文件的地址 15 } 16 } 17 } 18 }