Open file using OpenFileDialog Class

Sep 28 2009 2:15 PM

I am trying to do something very simple but am having a hard time making it work. I need to open a browse file dialog onclick of a button (windows application) and allow the user to select a file from a specified directory and open the file when onclick of the open button in the open file dialog. My code is below. For some reason it doesnt work. Please help!
private
void btnHandouts_Click(object sender, EventArgs e)
{
string
fileNamePath;
string fileNameOnly;
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title =
"Open File";
fdlg.InitialDirectory =
@"..\Handouts";
fdlg.Filter =
"All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.CheckFileExists =
true;
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory =
true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
fileNamePath = fdlg.FileName;
fileNameOnly =
Path.GetFileName(fileNamePath);
FileStream fileStream = new FileStream(@"..\Handouts\" + fileNameOnly, FileMode.Open);
fileStream.Close();
}
}

Answers (4)