Click here to Skip to main content
15,885,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir/Madam,

here in the below path with square brackets how can i get the

filename "Sample.doc".

[D:\Employee management system\EMP details11\Sample.doc]
Posted

This is fairly easy to do.

Give it a try - using Basic String Operations[^] should give you an idea.
If you still run into issues, post them here and someone will help you.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Nov-11 2:13am    
Makes sense, voted 5, but it does not teach good practical code. For example, please see my comment to the answer by JF2015.
--SA
Abhinav S 21-Nov-11 3:12am    
Yes true. Thanks for the 5 though.
Here is one quick hack that gives you the solution (C#):
C#
string test = @"[D:\Employee management system\EMP details11\Sample.doc]";
string res = test.Replace("[", "");
res = test.Replace("]", "");
res = System.IO.Path.GetFileName(res); //res gives you "sample.doc"

VB.NET:
VB
Dim test As String = "[D:\Employee management system\EMP details11\Sample.doc]"
Dim res As String = test.Replace("[", "")
res = test.Replace("]", "")
res = System.IO.Path.GetFileName(res) 'res gives you "sample.doc"

and here is another one (C#):
C#
string test = @"[D:\Employee management system\EMP details11\Sample.doc]";
string res = test.Replace("[", "");
res = test.Replace("]", "");
string[] splits = res.Split('\\');
res = splits[splits.Length - 1]; //res gives you "sample.doc"

VB.NET:
VB
Dim test As String = "[D:\Employee management system\EMP details11\Sample.doc]"
Dim res As String = test.Replace("[", "")
res = test.Replace("]", "")
Dim splits() As String = res.Split(Microsoft.VisualBasic.ChrW(92))
res = splits((splits.Length - 1))'res gives you "sample.doc"
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 21-Nov-11 2:11am    
I voted 4. First part is not so good. For removing [] use string.Trim.
--SA
JF2015 21-Nov-11 2:15am    
Yes, you are totally right!! Thanks for pointing that out.
you can get as
C#
string ss=@"D:\Employee management system\EMP details11\Sample.doc";
            string[] split = ss.Split(new Char[] { '\\' });

            ArrayList al = new ArrayList();

            foreach (string s in split)
            {

                if (s.Trim() != "")
                    al.Add(s);
            }
            Console.WriteLine(al[3].ToString());
 
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