using System; using System.Windows.Forms; namespace Example { class Program { [STAThread] static void Main() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { MessageBox.Show("Selected file: " + openFileDialog.FileName); } } } }
using System; using System.IO; using System.Windows.Forms; namespace Example { class Program { [STAThread] static void Main() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); if (openFileDialog.ShowDialog() == DialogResult.OK) { MessageBox.Show("Selected file: " + openFileDialog.FileName); } } } }In this example, we have set an initial directory for the OpenFileDialog to open in. The initial directory is set to the desktop. Once a file is selected, a message box is displayed with the selected file's name. Package library: System.Windows.Forms