How To: Extract file name form file path using C#

Problem

I am developing a WinForm application using C# and I want to copy a file from one directory to another using System.IO.File.Copy(). I have the file's full path, including file name. I need to extract the file name out of this string. What is the easiest way to do this?

Solution 

Of course, the first thing that came to mind was using regular expressions, or perhaps finding the index of the last backslash and removing all of the charcters using the string object's Remove function. However, this all seemed like too much work.

I decided to do a quick search on Google and found out from here that C# let's you simply do this:

System.IO.Path.GetFileName(filePath);

Fantasticly simple!

Published Tuesday, July 15, 2008 12:05 PM by Tod Birdsall
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Picture of Tod Birdsall

Welcome!

This is the personal website of Tod Birdsall, a programmer, business analyst, and gamer. Not necessarily in that order. Learn more...

Subscribe

Be notified of my latest posts. Subscribe to my blog.