Click here to Skip to main content
15,917,321 members
Please Sign up or sign in to vote.
2.20/5 (3 votes)
See more:
Hello ,
Please demonstrate to me how i can use the open file dialog box.
I need to as the user to select a file , one the user selects the file i needs to get its file path. how can i do this.
Posted

Hi..
Before asking this question you must have googled it ....

OpenFile DialogControl


<pre lang="C++">
OpenFileDialog ofd = new OpenFileDialog();
 string filename = "";
 string path = "";        
  if (ofd.ShowDialog() == DialogResult.OK)      
    {                  
                filename = System.IO.Path.GetFileName(ofd.FileName);   
                  path = System.IO.Path.GetDirectoryName(ofd.FileName);     
     }     
     MessageBox.Show(filename, "Filename");      
    MessageBox.Show(path, "Directory");


 
Share this answer
 
Hi,

here is some code

C#
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    MessageBox.Show(openFileDialog1.FileName);
}


If you need to get the path only, just search for the last "\" in the FileName.

Hope This helps
 
Share this answer
 
Comments
William Winner 1-Jul-10 0:50am    
or you could use the Path methods in System.IO.
Create object of OpenFileDialog
OpenFileDialog opDialog = new OpenFileDialog();


Handle the FilePk event

opDialog.FileOk += new CancelEventHandler(opDialog_FileOk);


show the dialog
opDialog.ShowDialog();

handler function
C#
void opDialog_FileOk(object sender, CancelEventArgs e)
    {
      string file_Name = opDialog.FileName;
    }
 
Share this answer
 
v2
Well, doing this [^]gave me these:
How to: Open Files Using the OpenFileDialog Component[^]
File Open Dialog box in C#[^]
There are many more if needed.
 
Share this answer
 
i hope this will work .
if you want only the selected file name then use the following code


 if OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK  then 
 messagebox.show(openfiledialog1.filename)
end if



if you want a selected file path then use following code
dim folder as string
dim files as array
if OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK  then
  folder = OpenFileDialog1.FileName
  files = Directory.GetFiles(folder)
end if
 
Share this answer
 
Comments
ridoy 22-Dec-14 4:08am    
is it for c#?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900