views:

34

answers:

3

I have the following string:

string path = "C:\Users\Username\Desktop\FileName.pdf";

I need to take off FileName.pdf and put in a string variable called fileName.

The code has to be generic in a sense that anytime i get path, i start going from the end of the string down to the first backslash.

I'm using C#

A: 

Use a System.IO.FileInfo object - you can get the filename using this easily.

Will A
Once you've taken off the filename, I'm sure you can then using System.IO.DirectoryInfo if you need to rip out the directory names. You might also want to look at String.Split and split on backslashes if you're after a slightly less safe approach. :)
Will A
Thanks...........
Beginner_Pal
A: 

Well you can use Split('\') and you get the last of the array

David Fortin
+5  A: 

You can use System.IO.Path to manipulate a path.

GetFileName Returns the file name and extension of the specified path string.

For completeness here's the code:

string fileName = Path.GetFileName(path);
ChrisF
Good. Worked...........
Beginner_Pal