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

  • Home
  • SEARCH
  • 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 8846405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:58:33+00:00 2026-06-14T11:58:33+00:00

I have a path $/Folder1/Folder2/Folder3/File.xml I would like to get the path minus File.xml

  • 0

I have a path “$/Folder1/Folder2/Folder3/File.xml” I would like to get the path minus “File.xml” i.e. “$/Folder1/Folder2/Folder3”.

I have written the following method,

public string GetFilePathFromFolderPath(string serverPath)
    {
        var folders = serverPath.Split('/').ToList();

        folders.RemoveAt(folders.Count - 1);


        return folders.Aggregate(string.Empty,
                                 (current, folder) =>
                                 !string.IsNullOrEmpty(current)
                                     ? string.Format("{0}/{1}", current, folder)
                                     : string.Format("{0}", folder));
    }

Is there a better way to do this?

My Unit Test works fine but I would like to know if there is a simple way…

[TestMethod()]
    public void GetRootPathFromConfigFilePath_Validate()
    {
        var t = new Twrar();
        var a = t.GetFilePathFromFolderPath("$/Quan/Maa/CSr/mai.py");
        Assert.IsTrue(a == "$/Quan/Maa/CSr");
    }
  • 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-14T11:58:34+00:00Added an answer on June 14, 2026 at 11:58 am

    I assume you mean to use “/” as the path separator character even though it is “\” for Windows.

    Dim s = "$/Quan/Maa/CSr///mai.py"
    Console.WriteLine(System.IO.Path.GetDirectoryName(s).Replace("\"c, "/"c))
    

    outputs $/Quan/Maa/CSr

    The following is Tarun Arora’s edit:

    For C# this should be…

    public string GetFilePathFromFolderPath(string serverPath)
        {            
            return Path.GetDirectoryName(serverPath).Replace("\\", "/");
        }
    

    And all of my unit tests pass this…

    [TestMethod()]
        public void GetRootPathFromConfigFilePath_Validate()
        {
            var t = new TfsWrapper();
            var a = t.GetFilePathFromFolderPath("$/Quan/Maa/CSr/mai.py");
    
            Assert.IsTrue(a == "$/Quan/Maa/CSr");
        }
    
        [TestMethod()]
        public void GetRootPathFromConfigFilePath_SmallPath()
        {
            var t = new TfsWrapper();
            var a = t.GetFilePathFromFolderPath("$/Quan/mai.py");
    
            Assert.IsTrue(a == "$/Quan");
        }
    
        [TestMethod()]
        public void GetRootPathFromConfigFilePath_RootPath()
        {
            var t = new TfsWrapper();
            var a = t.GetFilePathFromFolderPath("$/mai.py");
    
            Assert.IsTrue(a == "$");
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following records in my projects's .classpath file: <classpathentry kind=src path=src/main/webapp output=build/webapp/>
I have the local file path as c:\new folder\pdf\today\k.pdf I want to replace the
I have a folder path stored in the property nightly.basepath (here the value would
I have a path like C:\application\photo\gallery\sketches . Now I need to check whether this
I have a path like this: parent/child/reply How do I use PHP to remove
I have nearly 15 and more xml file inside of one folder if possible
I'd like to take a xml file from my classpath to unmarshal it and
In my application I have a config.xml file which is being parsed,. In this
For example if I have an XML file (exported from another app, so I
I have a SWF file located at the local network in the public shared

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.