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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:04:13+00:00 2026-05-16T21:04:13+00:00

I want to determine if a folder contains a file, when both are specified

  • 0

I want to determine if a folder contains a file, when both are specified by a path.

At first glance this seems simple. Just check if the file path starts with the directory path. However, this naive check ignores several issues:

  • Paths may be relative or absolute
  • Paths may use the alternate directory separator
  • Paths may use inconsistent casing, which matters depending on the OS
  • Different paths may refer to the same location
  • Probably some more that I don’t know about

Is there an existing method in the framework, or do I have to write my own?

  • 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-16T21:04:14+00:00Added an answer on May 16, 2026 at 9:04 pm

    As far as I know, there is no built-in .NET method to do this, but the following function should accomplish this using the FileInfo and DirectoryInfo classes:

    public static bool FolderContainsFile(String folder, String file)
    {
        //Create FileInfo and DirectoryInfo objects
        FileInfo fileInfo = new FileInfo(file);
        DirectoryInfo dirInfo = new DirectoryInfo(folder);
    
        DirectoryInfo currentDirectory = fileInfo.Directory;
        if (dirInfo.Equals(currentDirectory))
            return true;
    
        while (currentDirectory.Parent != null)
        {
            currentDirectory = currentDirectory.Parent;
    
            if(currentDirectory.Equals(dirInfo)
                return true;
        }
    
        return false;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check remote folder's content, and determine if the particular file exists
I want to determine which part of audio file contain speech or music. I
Given this string random_string= '4' i want to determine if its an integer, a
Any way to determine file age of an image in a folder using PHP?
I want to basically make a simple picture upload, then change the first picture
I want to be able to determine the output folder based on the platform
I want to determine the URL of a page in Javascript. Is this possible?
I have a ftp client that allows users to upload files. I want determine
I want to determine the screen resolution of the client machine and the set
I want to determine the scroll of the users. I'm using jQuery.. And jquery

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.