我有一个TextBoxnamedtextbox1和一个Buttonnamed button1。当我单击时,button1我想浏览我的文件以仅搜索图像文件(类型 jpg、png、bmp…)。当我选择一个图像文件并在文件对话框中单击确定时,我希望将文件目录写成textbox1.text这样:
TextBox
textbox1
Button
button1
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; } }