打开文件对话框,并使用WPF控件和C#选择一个文件 [英] Open file dialog and select a file using WPF controls and C#
本文介绍了打开文件对话框,并使用WPF控件和C#选择一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框
名为 TextBox1中
和按钮
名为按钮1
。
当我点击按钮1
我想浏览我的文件只搜索图像文件(JPG类型,PNG,BMP ......)。
当我选择一个图像文件,并在文件对话框我想在要写入的文件目录单击确定 textbox1.text
是这样的:
textbox1.Text =C:\\ MyFolder文件\\ myImage.jpg这个参数
解决方案
类似的东西应该是你所需要的。
私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
//创建打开文件对话框
Microsoft.Win32.OpenFileDialog DLG =新Microsoft.Win32.OpenFileDialog(); //文件扩展名和默认文件扩展名设置过滤器
dlg.DefaultExt =png格式;
dlg.Filter =JPEG文件(* .JPEG)| * .JPEG | PNG文件格式(* .png)| *。PNG | JPG文件(* .JPG)| * .JPG | GIF文件(* .gif注意)| * .gif注意;
//显示调用ShowDialog方法打开文件对话框
可空<布尔>结果= dlg.ShowDialog();
//获取选中的文件名,并显示在一个TextBox
如果(结果==真)
{
//打开文件
字符串文件名= dlg.FileName;
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:\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;
}
}
这篇关于打开文件对话框,并使用WPF控件和C#选择一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文