Scott Lock's Blog

Building the .Net Community through Caparea.Net, Consulting, and no sleep
posts - 62, comments - 30, trackbacks - 35

My Links

News

Twitter












Archives

Post Categories

Blogs I Read

Microsoft MVP

User Groups

Getting the filename from a path string - System.IO.Path to the rescue

I was looking for something simple to get the file name for the selected file of an OpenFileDialog in C# today.  Of course you can parse the string looking for backslash, etc. It annoyed me that there was no FileText property or something similar which returns this little nugget.

After a short Google search, I found that the System.IO namespace contains Path.GetFileName.  This helpful little static method returns the filename given a path (Path.GetFileName(string path);)   (supported in 1.x, 2.0, 3.0).  If you dig a little deeper, you will see that the Path class has a nice collection of helper methods for getting information about a file, its path, and its directory.  If you reflect on the function, you see that it simply does the parsing for you.

I am sure that every .Net developer already knows this, but its funny how it did not come up very easily when I did my search.

Print | posted on Thursday, March 29, 2007 10:22 PM |

Feedback

Gravatar

# re: Getting the filename from a path string - System.IO.Path to the rescue

Thanks for the tidbit! I, too, was looking for this exact thing, thinking that .NET *had* to have something that did this for me. Thanks again!
6/20/2007 5:48 PM | Shawn Dorman
Gravatar

# re: Getting the filename from a path string - System.IO.Path to the rescue


Your post showed up on page two of Google using

C# parsing filename opendialog

and the first to answer what I was looking for.

Thanks
9/18/2007 12:04 PM | Dan
Gravatar

# re: Getting the filename from a path string - System.IO.Path to the rescue

I searched google for "c# get filename from path string". This was the third result, and what I was looking for. Thanks.
12/12/2007 5:29 AM | foobar
Gravatar

# re: Getting the filename from a path string - System.IO.Path to the rescue

it worked for me
gr8
first result from google and right on target for me :)
7/21/2008 3:55 AM | kamran
Gravatar

# re: Getting the filename from a path string - System.IO.Path to the rescue

Googled "c# filename parsing" and was third result. Very helpful! Thanks!
9/18/2008 4:27 PM | Mike

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 1 and type the answer here:

Powered by: