打开文件对话框,并使用WPF控件和C#select一个文件

我有一个名为textbox1TextBox和一个名为button1Button 。 当我点击button1我想浏览我的文件,只search图像文件(typesjpg,png,bmp …)。 当我select一个图像文件,并在文件对话框中单击确定,我想文件目录被写入textbox1.text像这样:

 textbox1.Text = "C:\myfolder\myimage.jpg" 

像这样的东西应该是你所需要的

 private void button1_Click(object sender, RoutedEventArgs e) { // Create OpenFileDialog Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dlg.DefaultExt = ".png"; dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"; // Display OpenFileDialog by calling ShowDialog method Nullable<bool> result = dlg.ShowDialog(); // Get the selected file name and display in a TextBox if (result == true) { // Open document string filename = dlg.FileName; textBox1.Text = filename; } } 
 var ofd = new Microsoft.Win32.OpenFileDialog() {Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"}; var result = ofd.ShowDialog(); if (result == false) return; textBox1.Text = ofd.FileName;