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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:30:59+00:00 2026-06-05T23:30:59+00:00

I have the following code, I receive an error at if statement saying that

  • 0

I have the following code, I receive an error at “if statement” saying that FileInfo does not contain a definition “Contains”

Which is the best solution for looking if a file is in a directory?

Thanks

string filePath = @"C:\Users\";
DirectoryInfo folderRoot = new DirectoryInfo(filePath);
FileInfo[] fileList = folderRoot.GetFiles();

IEnumerable<FileInfo> result = from file in fileList where file.Name == "test.txt" select file;
if (fileList.Contains(result))
{
      //dosomething
}
  • 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-05T23:31:01+00:00Added an answer on June 5, 2026 at 11:31 pm

    Remove fileList.Contains(result) and use:

    if (result.Any())
    {
    
    }
    

    .Any() is a LINQ keyword for determining whether result has any items in it or not. Kinda like doing a .Count() > 0, except quicker. With .Any(), as soon as an element is found the sequence is no longer enumerated, as the result is True.

    In fact, you could remove the last five lines of your code from from file in... to the bottom, replacing it with:

    if (fileList.Any(x => x.Name == "test.txt"))
    {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code sample, which produces an error with the included constant
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code using hibernate to throw a custom exception on error and
I have the following code: print <a href='.preg_replace($replace, '-', $name.-pid-.$id..html'>; and receive the following
Evening all. I have the following code that I need looking into - basically
I have the following JavaScript code: aw_check_custom_validation['my-phone'] = /^\+[0-9]{1,3}(\s[0-9]{1,6}\s|\([0-9]{1,6}\)|[0-9]{1,6})[0-9\s]{5,8}$/u; and I receive the following
I have the following code which seems to throw a Invalid cross-thread access. and
I have the following code that determines if it's the first time the user
I have the following code in an aspx file that I cannot get to
Anyone have any ideas why the following code would produce an error, see additional

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.