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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:51:53+00:00 2026-05-15T19:51:53+00:00

Possible Duplicate: How can I compare (directory) paths in C#? I have a filename

  • 0

Possible Duplicate:
How can I compare (directory) paths in C#?

I have a filename and a directory name and I want to determent if the file is in the directory. My first thought was to use string comparison and check that the filename string starts with the directory name string. However, that would fail in the case where the directory name was a UNC path and the filename was a mapped drive. Or if there were some other form of alias in one of the strings.

The string comparison just doesn’t seem like a reliable method. Is there a built in .NET function for determining if 2 ‘DirectoryInfo’ objects are pointing to the same folder?

  • 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-15T19:51:54+00:00Added an answer on May 15, 2026 at 7:51 pm

    You should use the Path class.

    Something like the following would do the trick:

    string.Compare(Path.GetDirectoryName(filePath), directoryPath.Trim('\\'), true)
    

    If you wanted to handle relative paths then you can convert directoryPath and filePath into full paths first:

    string.Compare(Path.GetDirectoryName(Path.GetFullPath(filePath)), GetFullPath(directoryPath).Trim('\\'), true)
    

    EDIT: Edited to perform case invariant comparisons.

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

Sidebar

Related Questions

Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: How to compare two word documents? How can you get the diff
Possible Duplicate: Subtracting long numbers in javascript Can anyone tell me how to compare
Possible Duplicate: Can you call Directory.GetFiles() with multiple filters? How do you filter on
Possible Duplicate: Can I export and import Visual Studio 2010-extensions? I want to reinstall
Possible Duplicate: How can I compare two dates, return a number of days I
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I
Possible Duplicate: How can I split an IEnumerable<String> into groups of IEnumerable<string> I have
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got

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.