var openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { // Selected file path string filePath = openFileDialog.FileName; }
var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; openFileDialog.InitialDirectory = @"C:\"; openFileDialog.Title = "Select a Text File"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; }This example customizes the OpenFileDialog by setting the filter to only show text files and adding an option to select all file types. The initial directory is set to the C drive and the dialog title is changed to request a text file specifically. Overall, the OpenFileDialog class in C# provides an easy way for users to select files from their computer and can be customized to fit specific needs.