OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string path = openFileDialog.FileName; StreamReader reader = new StreamReader(path); string text = reader.ReadToEnd(); reader.Close(); }
OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image files (*.png;*.jpg;*.jpeg)|*.png;*.jpg;*.jpeg|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string path = openFileDialog.FileName; Image image = Image.FromFile(path); pictureBox1.Image = image; }This example demonstrates how to open an image file using the OpenFileDialog. We create an instance of the OpenFileDialog and set the Filter property to allow the user to select only image files. If the user selects a file and clicks the OK button, we retrieve the file path using the FileName property. We then create an instance of the Image class and load the image file using the FromFile() method. Finally, we display the image on a PictureBox control by setting the Image property. The Image class belongs to the System.Drawing namespace. Package/Library: The OpenFileDialog class is part of the .NET Framework and does not require any additional package or library.