小编典典

打开文件对话框,然后使用WPF控件和C#选择文件

c#

我有一个TextBox名字textbox1和一个Button名字button1。当我单击时,button1我想浏览我的文件以仅搜索图像文件(键入jpg,png,bmp
…)。当我选择一个图像文件并在文件对话框中单击“确定”时,我希望这样写入文件目录textbox1.text

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

阅读 753

收藏
2020-05-19

共1个答案

小编典典

那样的东西应该是您所需要的

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;
    }
}
2020-05-19