Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
2.20/5 (4 votes)
See more:
I am getting full path of a file which is present in the system. But I want only the file name with extension.. Any body tell me the code..
Posted
Updated 30-May-14 2:36am
v2
Comments
King Fisher 30-May-14 9:24am    
adequate solutions to this Question . Accept any solution and Remove the Question from unanswered List :)
RDBurmon 30-May-14 10:05am    
OP, this type of solution can be found on www.google.com with very easy search. Please search before posting question

Use Path.GetFileName. Also, this is an easy thing to google for to find yourself so that you do not have to wait on others.

Path.GetFileName Method[^]
 
Share this answer
 
v3
Comments
Nirav Prabtani 30-May-14 9:00am    
i have voted 5+ becuase some one voted it down.. :)
ZurdoDev 30-May-14 9:01am    
I noticed someone voted 1 on all solutions.
See the following code:
C#
string a = "C:\\example.txt";
string b = Path.GetFileName(a);

Then the value of b is "example.txt". Use Path.GetFileNameWithoutExtension to remove the extension also.

Note: Don't forget to add using System.IO; to use this code.
 
Share this answer
 
Comments
ZurdoDev 30-May-14 8:59am    
+5. Good explanation.
Use System.IO.Path.GetFileName(fullfilepath) to get the only FileName that you want to get .
 
Share this answer
 
try this.. :)

C#
string fileName = System.IO.Path.GetFileName("Full path with filename");
 
Share this answer
 
Comments
ZurdoDev 30-May-14 8:59am    
Not sure why someone voted 1 on yours. +5 to compensate.
Nirav Prabtani 30-May-14 9:01am    
ohhhh at a same time i have voted for you before i have seen your comment.. :)
Here is your solution

C#
string path = @"E:\Test\ovd.txt";
string result;

result = Path.GetFileName(path);
MessageBox.Show(result.ToString());
 
Share this answer
 
I nearly always use a FileInfo:

System.IO.FileInfo fi = new System.IO.FileInfo ( whatever ) ;

It has several properties, including FileName.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900