[英]Open file dialog and select a file using WPF controls and C#
I have a TextBox
named textbox1
and a Button
named button1
. 我有一个名为
textbox1
的TextBox
和一个名为button1
的Button
。 When I click on button1
I want to browse my files to search only for image files (type jpg, png, bmp...). 当我点击
button1
我想浏览我的文件以仅搜索图像文件(类型为jpg,png,bmp ...)。 And when I select an image file and click Ok in the file dialog I want the file directory to be written in the textbox1.text
like this: 当我选择一个图像文件并在文件对话框中单击“确定”时,我希望将文件目录写入
textbox1.text
如下所示:
textbox1.Text = "C:\myfolder\myimage.jpg"
Something like that should be what you need 这样的事情应该是你需要的
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.