1. References

https://stackoverflow.com/questions/10315188/open-file-dialog-and-select-a-file-using-wpf-controls-and-c-sharp



2. Source 


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;

    }

}

반응형

+ Recent posts