Geeks With Blogs
Scott Lock Thoughts on .Net, Caparea.net and Windows Phone

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.

Posted on Thursday, March 29, 2007 10:22 PM | Back to top


Comments on this post: Getting the filename from a path string - System.IO.Path to the rescue

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
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!
Left by Shawn Dorman on Jun 20, 2007 5:48 PM

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

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
Left by Dan on Sep 18, 2007 12:04 PM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
I searched google for "c# get filename from path string". This was the third result, and what I was looking for. Thanks.
Left by foobar on Dec 12, 2007 5:29 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
it worked for me
gr8
first result from google and right on target for me :)
Left by kamran on Jul 21, 2008 3:55 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
Nice piece of information. Thanks
Left by Anil on Dec 08, 2008 1:35 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
I was hoping someone would post this up somewhere easy. Gotta love Google!
Thanks!
Left by Aikeru on Apr 14, 2009 12:44 PM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
It's strange how this isn't made more clear in the Microsoft documentation. Fortunately most developers can Google...
Left by Aenikata on Jun 15, 2009 5:33 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
1st result for ".net get filename from path" Thanks :)
Left by anon on Aug 03, 2009 7:09 PM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
Thank you very much! This was the second hit on googling for "vb.net parse filename from path"
Left by Marlin on Nov 08, 2009 12:40 PM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
This post is over 3.5 years old and is still helping people. Not bad. Thanks.
Left by David on Oct 07, 2010 6:56 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
saves a load of 'manual' parsing when retrieving
files using FTP - tx
Left by anon on Nov 03, 2010 3:19 PM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
Thank you very much that's exactly what I was looking for
Left by ON4LPF on Jan 01, 2011 2:44 PM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
Thank you very much
Left by AQuatember on Feb 28, 2011 9:43 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
Thanks - wasn't sure how to do this the easy way - I also need to remove the filetype suffix (in my case '.xml', so if anyone else needs to do that it's:
System.IO.Path.GetFileName(Filename).Replace(".xml","");
cheers,

Stephen
Left by Steve on May 06, 2011 8:57 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
..Or you can use the GetFileNameWithoutExtension function which I just noticed (doh!)!
Left by Steve on May 06, 2011 9:00 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
I am a .Net developer and didn't know about this. Thanks it is great information
Left by Carlos Roque on Aug 31, 2011 8:36 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
Small tips like these save time. I could have looked it up the hard way by examining the help files but hey, you must have saved me 5-10 minutes.
Thanks!
Left by Tarik on Jun 12, 2012 3:56 AM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
What version of .Net are you using?... because since 3.5, OpenFileDialog.SafeFileName property returns just name and extension.
Left by tlum on Dec 04, 2012 1:44 PM

# re: Getting the filename from a path string - System.IO.Path to the rescue
Requesting Gravatar...
I have forgot about this. Good one.
Left by Momo on Dec 06, 2012 8:32 AM

Your comment:
 (will show your gravatar)


Copyright © Scott Lock | Powered by: GeeksWithBlogs.net