How to read Directories based on sequence order by static list

Viewed 29

I have Directories(static) called America, India, Japan. Directory.GetDirectories method should work based on sequence order like Japan, America, India


 Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "Japan");
dic.Add(2, "America");
dic.Add(3, "India");


 foreach(var Country in Directory.GetDirectories("C://", "*",System.IO.SearchOption.TopDirectoryOnly)))
 {
    string countryName = new DirectoryInfo(Country).Name;
    Console.Writeline(countryName);
 }


// Output should be below

 // Japan
 // America
 // India

1 Answers

Instead of using the directory list to advance your loop, use the dictionary values and then process those values with DirectoryInfo

Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "Japan");
dic.Add(2, "America");
dic.Add(3, "India"); // <= this should have 3 as key value.

foreach (var country in dic.Values)
{
    if (Directory.Exists(Path.Combine(@"C:\", country)))
    {
        string countryName = new DirectoryInfo(country).Name;
        Console.WriteLine(countryName);
    }
}
Related