Click here to Skip to main content
15,884,758 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I have a path
d:\myfolder\myTest\testfolder\111111\2222

I need to get the last part of the path i.e 2222

Please help me.
Posted
Updated 14-Jun-12 20:37pm
v2
Comments
Sergey Alexandrovich Kryukov 15-Jun-12 1:18am    
How about just reading your help? It's way faster then asking such questions.
--SA

 
Share this answer
 
There are a number of ways: You could use string methods, or FileInfo methods, depending on what "2222" is:
C#
string path = @"d:\myfolder\myTest\testfolder\111111\2222";
FileInfo fi = new FileInfo(path);
Console.WriteLine(fi.Name);
string last = path.Substring(path.LastIndexOf('\\') + 1);
Console.WriteLine(last);
In fact, both methods produce the same result, and neither of them require the file of even the folder structure / disk to exist. However, the FileInfo method will require the path to be a valid file specification - it will throw an exception if you give it ":D\myfolder\myTest\testfolder\111111\2222" for example.
 
Share this answer
 
hi,
Using String.Substring() you can do it easily,
C#
string path = @"d:\myfolder\myTest\testfolder\111111\2222";
          path = path.Substring(path.LastIndexOf(@"\") + 1);
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 15-Jun-12 1:37am    
Why?!
--SA
tanweer 15-Jun-12 2:53am    
whats wrong with my this answer??
Get files from directory (with specified extension):

You can specify search pattern. You can use wildcard specifiers in the search pattern, e.g. „*.bmp“ to select files with the extension or „a*“ to select files beginning with letter „a“.


string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");
// returns:
// "c:\MyDir\my-car.BMP"
 
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