Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I get just the filename of the Open File Dialog 3

Status
Not open for further replies.

vincentw56

IS-IT--Management
Oct 10, 2002
47
0
0
US
How can I get just the filename of the Open File Dialog? If I use txtFileName.Text = OpenFileDialog1.FileName.ToString, it gives me the path and filename. I would like to just get the file name without the path. I am creating a list so when a file is added, it will parse the file name for the new file to create. Thanks.

Vincent
 
Yes, OpenFileDialog1.FileName always gives you the path + filename and I don't think that there is any other property that gives you just filename. This is what I did to get the filename

Code:
sFileName = sFullPath.Substring(sFullPath.LastIndexOf("\") + 1, (sFullPath.Length - sFullPath.LastIndexOf("\") - 1))

-Kris
 
or....
dim sArray as String()=sfullpath.split("\"c)
dim sFileName as String = sArray(sArray.length - 1)


Sweep
...if it works dont mess with it
 
Just for reference I found another way to do it.

Code:
System.IO.Path.GetFileName(sFolder)

Thanks for the help.
 
I didn't know that, thatz a better way of doing it. Thanks for sharing it.

-Kris
 
Vincentw56's answer is the best one, as the path separater may not always be a backslash character (Longhorn might change this, as they have a new filesystem!). By letting the runtime do the work, your code will work even on Longhorn, or on Unix if you decide to run Mono.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top