0

Below is my string. I want to do the following kind of operation: If my url contains "Destinationfolder" then I have to get folder1 and folder2, and the result has to assign to another string one by one.

string strpath = @"D:\Multilingual\Destinationfolder\folder1\folder2";

4 Answers 4

3

You could split by \

if(strpath.Contains("Destinationfolder")){
    var parts = @"some\stuff".Split('\\').ToList();
    var i = parts.IndexOf("Destinationfolder");
    var folder1 = parts[i+1];
    var folder2 = parts[i+2];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could use the Path.GetDirectoryName method in a loop.

Comments

0
var folders = strpath.Substring(strpath.IndexOf(@"Destinationfolder\") + 
                               @"Destinationfolder\".Length).Split('\\');

Comments

0
string strpath = @"D:\Multilingual\Destinationfolder\folder1\folder2";
string folderToFind = "Destinationfolder";

var subfolders = new List<string>();
if (strpath.Contains("Destinationfolder"))
{
    subfolders.AddRange(Regex.Replace(strpath, @".*"+folderToFind, "")
         .Trim('\\')
         .Split('\\'));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.