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();
}
}
Loading

Roei BarRank #1,271 · 1.4K reputationPosted Sep 28, 2009, 1:50 PM EDT
private void btnHandouts_Click(object sender, EventArgs e) { string fileName; 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) { Process.Start(fdlg.FileName); } }Roei BarRank #1,271 · 1.4K reputationPosted Sep 28, 2009, 12:29 PM EDT
private void btnHandouts_Click(object sender, EventArgs e) { string fileName; 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) { fileName = fdlg.FileName; FileStream fileStream = new FileStream(fileName, FileMode.Open); fileStream.Close(); } }the problem was in the path you built, it wasnt necessary since
Eugene Gabriyelov5 reputationPosted Sep 28, 2009, 1:09 PM EDT
Eugene Gabriyelov5 reputationPosted Sep 29, 2009, 7:21 AM EDT