Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i save only the last value of the path by split method
For eg: D:/Folder1/abc
OR
D:/Folder1/Folder2/abc

i need to save only "abc" as my value in string format where "abc" is my project name.

please help me how can i do this
i have try this method

string str1= "D:/Folder1/abc"
string[] split = str1.Split('/', ':');
C#
for (int i = 0; i<split.Length; i++)
       {

           Response.Write(split[i].ToString());//Check here for getting the value..

       }

but its not working how can i save only the last value of the path
Posted

Use System.IO.Path.GetFileName. It gets the last part of the string, and does not know if it was a filename, or the last folder name in a sequence.
 
Share this answer
 
Comments
priti2010 1-Aug-12 2:48am    
It should save Foldername not file name, i want to save to the RootDirectoryname as explain above
pradiprenushe 1-Aug-12 3:06am    
have you try this?
Christian Graus 1-Aug-12 2:51am    
So, read what I said, again. If you call GetFileName on a path, it will give you the last folder name in the path.
Hello,

Here is a way to do it:

C#
string str1= "D:/Folder1/abc";
string[] split = str1.Split("/");
string lastBit = split[split.Length-1];
Response.Write(lastBit);


But I think you may have your path separator the wrong way round. ie "\" not "/".

The your code is:

C#
string str1 = @"D:\Folder1\abc";
string[] split = str1.Split(Path.DirectorySeparatorChar);
string lastBit = split[split.Length - 1];
Response.Write(lastBit);





Valery.
 
Share this answer
 
Comments
priti2010 1-Aug-12 5:08am    
thanx

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