Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, can someone hlep me to reverse the below logic so that I'm selecting the chars after the final \\. For example in this string C:\inetpub\TESTAPP I want to get the substring TESTAPP as it is after the last instance of the \.

Here's the code I'm using at present which is returning 'inetpub':
C#
string dirpath = @"" + dir;
//Specs wants this to contain the name of the parent directory.
String ss = String.Empty;

int i = dir.LastIndexOf("\\");
ss = dir.Remove(i);
ss = ss.Substring(ss.LastIndexOf("\\") + 1);

string compressedDirpath = @"" + dir + "\\" + ss + ".gz";
string[] argd = { dirpath, compressedDirpath };
Posted
Updated 8-Oct-13 23:52pm
v2

1 solution

Hey there,

Try this to get TESTAPP:
C#
string dirpath = @"" + dir;
//Specs wants this to contain the name of the parent directory.
String ss = String.Empty;
 
ss = dir.Substring(dir.LastIndexOf("\\") + 1);
 
string compressedDirpath = @"" + dir + "\\" + ss + ".gz";
string[] argd = { dirpath, compressedDirpath };

I do not understand why you were doing dir.Remove(i), but it was causing the problem.

Hope it helps.

Azee...
 
Share this answer
 
v2
Comments
Bernhard Hiller 9-Oct-13 8:33am    
Better take a look at System.IO.Path, e.g. GetFilename and Combine methods.

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