views:

51

answers:

3

HI,

I am trying to retrive the filname of the image file from a file path in my code.

My filepath: c:\mydocuments\pictures\image.jpg

which method can i use in c# to get he filename of the above mentioned path.

Like String file = image.jpg

I have used the system.drawing to get he path, but it returns null.

my code:

string file = System.drawing.image.fromfile(filepath,true);

Is this the right way to get the image file name or is there any other inbuilt method in c#.

Pls help me on this?

A: 

Image.FromFile returns an Image object, not the filename.

If you just want to get the filename portion of a path, use System.IO.Path.GetFileName(path)

Josh Einstein
+1  A: 
System.IO.FileInfo info = new System.IO.FileInfo(filepath);
string fileName = info.Name;
Kyle Rozendo
Should that not be info.FullName?
uriDium
@uriDium: Nope, the OP asked for the filename without the path and in `FileInfo` you'll get it through the `Name` property.
Oliver
+1  A: 

Use Path.GetFileName

Franci Penov