Skip to content

How to retrieve the filename from openfiledialog

nicia

Asked Sep 1, 2005, 1:13 AM EDT3 answers
Share
LinkedInX

hi, i like to ask if there anyway that i can get the filename from openfiledialog. I just want the filename and not the all pathway. 
Below is the coding i have done.

openFileDialog.showDialog();
textfile = openFileDialog.FileName;

but this will give me the pathway of the file
e.g. (C:\Documents and Settings\Annicia\Desktop\annicia\SQL Server.exe) but i only want the filename (SQL Server.exe). 
If there anyway i can do it as i am quite new to c# can guide me along, thank. 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    int p=txtfile.LastIndexOf("\\"); if(p>=0) txtfile=txtfile.Substring(p+1);
    0
  • 0
    Use the Path class method: 
    Path.GetFileName( string FullPath).

    This returns the file name alone.
    0
  • 1 short reply
    • 0
      hi, thank for the help. =)
      0