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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:31:49+00:00 2026-06-09T23:31:49+00:00

While working on a project today I ran into this odd behavior in .NET.

  • 0

While working on a project today I ran into this odd behavior in .NET.

I’m grabbing a list of file names and adding them to an object. However I need to filter the incoming names against a known list of “bad” names which led me to do this. (I’m by no means an advanced C# coder, still just learning it.) However, because it causes the aspnet_wp.exe process to run at 100% indefinitely, I assume that I’m doing something wrong with the language.

Here’s my code for reference:

List<string> localFiles = new List<string>(); 
// I was worried the object was causing the issue so dumbed it down to this with no changed.

string path = "//<file share>/<dir>/"; 
// As I commented below yes, it's slow when it works but it's clearly not working when using the if();

List<string> omitNames = new List<string>();
omitNames.Add("Thumbs.db"); 
// This is the only item in the list when it breaks also.

FileInfo[] localFileList = new DirectoryInfo(path).GetFiles();
foreach ( FileInfo item in localFileList )
{
    if(!omitNames.Contains(item.Name))
    {
        localFiles.Add(path + itemName);
    }
}

Can anyone explain why this code runs as an infinite loop? It looks like it really shouldn’t. Also, I realize that using a List may not be the best approach. Is there another way to implement this cleanly?

Commenting out the if(!){} for omitNames allows the code to run properly. (Though obviously the results are not filtered.)

UPDATE: It was requested to put this in a console app which you’ll find below. However it works perfectly. Another thing is that someone suggested that I try to simply compare it against a string. But the exact same thing happens if it’s changed to this:

if(item.Name != "Thumbs.db")
{
    localFiles.Add(path + itemName);
}

Console App (which works):

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LoopKiller
{
class Program
{

    static void Main(string[] args)
    {

        List<string> omitNames = new List<string>();
        List<string> localFiles = new List<string>();

        omitNames.Add("Thumbs.db");

        FileInfo[] localFileList = new DirectoryInfo("c:/test/").GetFiles();

        foreach (FileInfo item in localFileList)
        {
            if (!omitNames.Contains(item.Name))
            {
                Console.WriteLine("Adding " + item.Name + " to localFiles.");
                localFiles.Add(item.Name);
                Console.WriteLine("Item added to localFiles.");
            }
        }

        foreach (string item in localFiles)
        {
            Console.WriteLine(item);
        }

        Console.ReadLine();

    }
}
}
  • 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-09T23:31:50+00:00Added an answer on June 9, 2026 at 11:31 pm

    Is this any quicker?

    foreach (string item in Directory.GetFiles("c:/test/", "*.*").Select(Path.GetFileName())
        {
            if (!omitNames.Contains(item))
            {
                Console.WriteLine("Adding " + item.Name + " to localFiles.");
                localFiles.Add(item.Name);
                Console.WriteLine("Item added to localFiles.");
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while working on a school project i ran into a problem using javascript to
So I ran into a problem today while working on my Android program. I
While working on a project I ran into the following Issue, in my CoreData
While working a project tonight, I ended up using one .js resource file for
While working on my final project for my AS/400 Course, I encountered this problem
recently, while working on a db2 -> oracle migration project, we came across this
So I have been working on this project for a short while now. I
While working on my project, I got this error: The item ... was specified
I am teaching myself jquery it seems while working on this project. I assumed
I've bumped into a problem while working at a project. I want to crawl

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.