打开文件对话框并使用 WPF 控件和 C# 选择文件 [英] Open file dialog and select a file using WPF controls and C#
本文介绍了打开文件对话框并使用 WPF 控件和 C# 选择文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 textbox1
的 TextBox
和一个名为 button1
的 Button
.当我单击 button1
时,我想浏览我的文件以仅搜索图像文件(类型 jpg、png、bmp...).当我选择一个图像文件并在文件对话框中单击确定时,我希望将文件目录写入 textbox1.text
中,如下所示:
I have a TextBox
named textbox1
and a Button
named button1
.
When I click on button1
I want to browse my files to search only for image files (type 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 = "C:myfoldermyimage.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;
}
}
这篇关于打开文件对话框并使用 WPF 控件和 C# 选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文