Scott Lock

Thoughts on .Net, Caparea.net and Windows Phone


News

My Stats

  • Posts - 77
  • Comments - 134
  • Trackbacks - 21

Tag Cloud


Recent Comments


Recent Posts


Archives


Post Categories


Image Galleries


Blogs I Read


Microsoft MVP


User Groups



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.


Comments

Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Shawn Dorman on 6/20/2007 5:48 PM
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!
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Dan on 9/18/2007 12:04 PM

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

Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by foobar on 12/12/2007 5:29 AM
I searched google for "c# get filename from path string". This was the third result, and what I was looking for. Thanks.
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by kamran on 7/21/2008 3:55 AM
it worked for me
gr8
first result from google and right on target for me :)
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Anil on 12/8/2008 1:35 AM
Nice piece of information. Thanks
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Aikeru on 4/14/2009 12:44 PM
I was hoping someone would post this up somewhere easy. Gotta love Google!
Thanks!
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Aenikata on 6/15/2009 5:33 AM
It's strange how this isn't made more clear in the Microsoft documentation. Fortunately most developers can Google...
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by anon on 8/3/2009 7:09 PM
1st result for ".net get filename from path" Thanks :)
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Marlin on 11/8/2009 12:40 PM
Thank you very much! This was the second hit on googling for "vb.net parse filename from path"
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by David on 10/7/2010 6:56 AM
This post is over 3.5 years old and is still helping people. Not bad. Thanks.
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by anon on 11/3/2010 3:19 PM
saves a load of 'manual' parsing when retrieving
files using FTP - tx
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by ON4LPF on 1/1/2011 2:44 PM
Thank you very much that's exactly what I was looking for
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by AQuatember on 2/28/2011 9:43 AM
Thank you very much
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Steve on 5/6/2011 8:57 AM
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
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Steve on 5/6/2011 9:00 AM
..Or you can use the GetFileNameWithoutExtension function which I just noticed (doh!)!
Gravatar # re: Getting the filename from a path string - System.IO.Path to the rescue
Posted by Carlos Roque on 8/31/2011 8:36 AM
I am a .Net developer and didn't know about this. Thanks it is great information
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: