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 6974525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:14:56+00:00 2026-05-27T17:14:56+00:00

What is the best way extract the common file path from the list of

  • 0

What is the best way extract the common file path from the list of file path strings in c#?

Eg:
I have a list 5 file paths in List variable, like below

c:\abc\pqr\tmp\sample\b.txt
c:\abc\pqr\tmp\new2\c1.txt
c:\abc\pqr\tmp\b2.txt
c:\abc\pqr\tmp\b3.txt
c:\abc\pqr\tmp\tmp2\b2.txt

output should be c:\abc\pqr\tmp

  • 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-05-27T17:14:57+00:00Added an answer on May 27, 2026 at 5:14 pm

    Because everything is best solved with LINQ*:
    *not everything is best solved with LINQ.

    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
            List<string> Files = new List<string>()
            {
                @"c:\abc\pqr\tmp\sample\b.txt",
                @"c:\abc\pqr\tmp\new2\c1.txt",
                @"c:\abc\pqr\tmp\b2.txt",
                @"c:\abc\pqr\tmp\b3.txt",
                @"c:\a.txt"
            };
    
            var MatchingChars =
                from len in Enumerable.Range(0, Files.Min(s => s.Length)).Reverse()
                let possibleMatch = Files.First().Substring(0, len)
                where Files.All(f => f.StartsWith(possibleMatch))
                select possibleMatch;
    
            var LongestDir = Path.GetDirectoryName(MatchingChars.First());
        }
    }
    

    Explanation:

    The first line gets a list of lengths of possible matches to evaluate. We want the longest possibility first (so i reverse the enumeration which would be 0, 1, 2, 3; turning it into 3, 2, 1, 0).

    I then get the string to match, which is simply a substring of the first entry of the given length.

    I then filter the results, to ensure we only include possible matches that all files start with.

    Finally, i return the first result, which will be the longest substring and call path.getdirectoryname to ensure if something has a few identical letters in the filenames it isn’t included.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to extract the key and value from a string like
In python, what is the best way to extract the list of items from
What is the best way to extract each field from each line where there
Whats the best way to extract a bit from an unsigned char .In my
What is the best way to extract date from SQL Datetime field in C#.Net?
What is the best way to extract the integer part of a string like
What's the best way to extract the related data from a has many through
I am looking for a way to extract an icon from a .exe file
What is the best way to extract RSS/ATOM URLs from HTML LINK tags? I
What is the best way to extract text from a pdf?

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.