Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:30:13+00:00 2026-06-08T20:30:13+00:00

Consider the path to a file like this: \\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml How can I make a

  • 0

Consider the path to a file like this:

“\\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml”

How can I make a new string that gives the path to the “project” folder in the string above? I tried to use FirstIndexOf("project") but it always fails!

Is there a static method or something in Directory or Path class?

I want to have a new string which contains the path to the “project” folder, from similar strings like above example, something like

var newPath = "\\\\Tests\\Results\\knowles\\project"

Please note, the “project” folder is just an example, it can be any name, what I ment is that I want to get the path to the folder that suppose to be in place of the project in my example.

Again, my mistake I guess, the original string does not come always with fixed number of folders, it is for sure does have atleast one or two folders after the ‘project’ folder…I need a way to approach it from left to right I guess

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:30:15+00:00Added an answer on June 8, 2026 at 8:30 pm

    You can use the FileInfo and DirectoryInfo class.

    Do a right to left search as follows:

    var fi = new FileInfo(""\\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml"");
    var di = fi.Directory;
    var find = "project";
    
    while (di.Name.ToLower() != find.ToLower() && di != null)
    {
        di = di.Parent;
    }
    
    if (di == null)
    {
        throw new Exception(string.Format("Directory with name '{0}' was not found.", find));
    }
    

    Do a left to right search with a recursive solution. Something like this:
    NOTE This is untested so may not be exactly right, but I think it’s generally what you are trying to achieve.

    var fi = new FileInfo(""\\Tests\Results\knowles\project\LU\D15\RUN1\Results.xml"");
    var di = fi.Directory;
    var find = "project";
    
    di = GetGreatestParent(di, find);
    
    if (di == null)
    {
        throw new Exception(string.Format("Directory with name '{0}' was not found.", find));
    }
    
    public DirectoryInfo GetGreatestParent(DirectoryInfo Dir, string Find)
    {
        if (Dir != null)
        {
            var p = GetGreatestParent(Dir.Parent, string Find);
    
            if (p != null)
            {
                return p;
            }
            else if (Dir.Name.ToLower() == Find.ToLower())
            {
                return Dir;
            }
        }
    
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up question to Converting file path to URI . Consider: require
I am planning my directory structure for a linux/apache/php web project like this: Only
I would like to know how an absolute path of a symbolic link can
I am working on a new PHP project now, this time I want to
This may seem like a silly question, but consider the following use of the
Consider the 2 following methods of reading a string from a file: NSString *path
Consider the class foo with two constructors defined like this: class foo { public:
I have written a very simple file managing database that basicly looks like this:
I'm relatively new to (GNU) Make, and find it incedibly difficult. I consider switching
Consider the following: with open(path, mode) as f: return [line for line in f

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.