This example uses a button click method.
private void btnOpenTextFile_Click(object sender, EventArgs
e)
{
//First, declare a variable to hold the user’s file
selection.
private void String input = string.Empty;
//Create a new instance of the OpenFileDialog because it's
an object.
private voidOpenFileDialog dialog = new OpenFileDialog();
//Now set the file type
dialog.Filter = "txt files (*.txt)|*.txt|All files
(*.*)|*.*";
//Set the starting directory and the title.
dialog.InitialDirectory = "C:"; dialog.Title = "Select a
text file";
//Present to the user.
if (dialog.ShowDialog() == DialogResult.OK)
strFileName = dialog.FileName;
if (strFileName == String.Empty)
return;//user didn't select a file
}
|