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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:01:58+00:00 2026-06-07T09:01:58+00:00

I am trying to create a SortedDictionary where its key is a string that

  • 0

I am trying to create a SortedDictionary where its key is a string that represents a File/Folder path. I am trying to sort that dictionary based on path depths. My criteria just checks for the number of slashes in each path and puts the path with the most slashes at the beginning of the dictionary.

My problem is that for some weird issue that I cant detect, the dictionary can have multiple keys that are exactly the same. The problem after hours of debugging seem to be that “sometimes” my IComparer implementation does not loop over all entries in the dictionary when calling ContainsKey method or when adding a new value to the dictionary. I don’t get an exception or anything.

This is my code.. (Its a bit long as):

 namespace ConsoleApplication1
{

    class DepthComparer : IComparer<string>
    {
        public int Compare(string X, string Y)
        {
            //Sort From deepest to shallowest 
            //C:\Users\NAME\Desktop\Folder\ should precede C:\Users\NAME\Desktop\
            //Paths with same root level are ignored

            int nXSlashes = SlashCounter(X);
            int nYSlashes = SlashCounter(Y);

            if (string.Compare(X, Y, true) == 0) //same path
            {
                return 0;
            }

            //Put Deepest Path at the beginning
            return (nXSlashes > nYSlashes ? -1 : 1);

        }

        public int SlashCounter(string stPath)
        {
            int nSlashes = 0;
            for (int i = 0; i < stPath.Length - 1; ++i)
            {
                if (stPath[i] == ('/') || stPath[i] == ('\\'))
                    nSlashes++;
            }
            return nSlashes;
        }
    }

    public class ScanOptions
    {
        public enum ExcludeRule
        {
            Invalid = 0x00,
            File = 0x01,
            Folder = 0x02,
            FileFolder = File | Folder,
        }

        private SortedDictionary<string, ExcludeRule> _dExcludedPaths;

        public ScanOptions()
        {
            _dExcludedPaths = null;            
        }

        //Creates a new Excluded Paths List (Automatically clears the current list if its already initialized)
        public void CreateExcludedPathsList()
        {
            if (_dExcludedPaths == null)
                _dExcludedPaths = new SortedDictionary<string, ExcludeRule>(new DepthComparer());
            else
                ClearExcludedPathsList();
        }

        public void ClearExcludedPathsList()
        {
            _dExcludedPaths.Clear();
        }

        public bool IsExcludedPathsListInitialized()
        {
            return _dExcludedPaths != null ? true : false;
        }

        public void AddExcludePath(string stPath, ExcludeRule Rule)
        {
            if (!IsExcludedPathsListInitialized())
                return;

            if (string.IsNullOrEmpty(stPath) || Rule == ExcludeRule.Invalid)
                return;

            string stTmp = stPath.ToLower();
            try
            {
                if (stTmp.EndsWith("\\"))
                {
                    stTmp = stTmp.Remove(stTmp.Length - 1);
                }

                if (_dExcludedPaths.ContainsKey(stTmp))
                {
                    ExcludeRule OldRule = ExcludeRule.Invalid;
                    if (_dExcludedPaths.TryGetValue(stTmp, out OldRule))
                    {
                        if ((OldRule & Rule) == 0)
                            _dExcludedPaths[stTmp] |= Rule; //Same path new rule, append rule to existing one
                    }
                    return;
                }
                else
                {
                    //brand new entry
                    _dExcludedPaths[stTmp] = Rule;
                }
            }
            catch
            {

            }
        }

        public void AddExcludePaths(List<string> ExcludePaths, ExcludeRule Rule)
        {
            if (!IsExcludedPathsListInitialized())
                return;

            foreach (string stPath in ExcludePaths)
                AddExcludePath(stPath, Rule);
        }

        public void AddExcludePaths(SortedDictionary<string, ExcludeRule> ExcludePaths)
        {
            if (!IsExcludedPathsListInitialized())
                return;

            foreach (KeyValuePair<string, ExcludeRule> PathRule in ExcludePaths)
                AddExcludePath(PathRule.Key, PathRule.Value);
        }


        public void ShowInConsole()
        {
           foreach (KeyValuePair<string, ScanOptions.ExcludeRule> Rule in _dExcludedPaths)
            {
                Console.WriteLine(Rule.Key + "\t" + Rule.Value);
            }
        }

    }

    class Program
    {
        static void Main(string[] args)
        {

            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            //Fill a new string list with some paths
            List<string> ExcludedPaths = new List<string>();
            string ExPath = desktopPath + "\\12345678\\";
            ExcludedPaths.Add(ExPath);
            ExPath = desktopPath + "\\abcdefg\\";
            ExcludedPaths.Add(ExPath);
            ExPath = desktopPath + "\\ABCDEFG\\";
            ExcludedPaths.Add(ExPath);
            ExPath = desktopPath + "\\1A2B3C 4D5E6F\\123456\\4567896\\";
            ExcludedPaths.Add(ExPath);
            ExPath = desktopPath + "\\CDEVQWER ASD\\456786\\";
            ExcludedPaths.Add(ExPath);

            //Create the exclude list object
            ScanOptions scanOpt = new ScanOptions();
            scanOpt.CreateExcludedPathsList();

            //First manually add one of the paths from the list above
            scanOpt.AddExcludePath(desktopPath + "\\12345678\\", ScanOptions.ExcludeRule.Folder);

            //Now add the whole list of paths to the exclude list in scan options
            scanOpt.AddExcludePaths(ExcludedPaths, ScanOptions.ExcludeRule.Folder);

            //Now add the first entry a couple more times with different value each time
            scanOpt.AddExcludePath(desktopPath + "\\12345678\\", ScanOptions.ExcludeRule.File);

            scanOpt.AddExcludePath(desktopPath + "\\12345678\\", ScanOptions.ExcludeRule.Folder);


            //Dump the list to console
            //We now have two keys in the dictionary that equal desktopPath + "\\12345678\\"
            scanOpt.ShowInConsole();

        }
    }
}

At the end of Main() _dExcludedPaths will have two of its keys exactly equal to each other.

Can someone help in understanding what is going on here and how is it possible to have duplicate keys in a dictionary??

Thanks

  • 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-07T09:02:00+00:00Added an answer on June 7, 2026 at 9:02 am
        public int Compare(string X, string Y) 
        {  
            int nXSlashes = SlashCounter(X); 
            int nYSlashes = SlashCounter(Y); 
    
            if (nXSlashes > nYSlashes)
              return -1;
            if (nXSlashes < nYSlashes)
              return 1;
    
            return string.Compare(X, Y, true);          
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying create this function such that if any key besides any of the
I'm trying to create a Temp Table that has a key field that auto
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a ASMX webservice that can perform a HTTP GET request. I
I'm trying create a new file with a Java Applet, but I don't know
I am trying create a small web application that allows a user to login
Trying to create a simple document that gets the parentNode then applies a background
Trying to create a pattern that matches an opening bracket and gets everything between
Trying to create a simple plugin that simply connects to an ftp site, looks
I'm trying create a gui using Tkinter that grabs a username and password and

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.