# Amending code to make it list some files in a directory, rather than all

The method list_all() is the code I am starting with. It just lists all files in "c:\windows".

I want to adjust it to list only the first 10 files (if there are as many files as that there).

I can see that Directory.GetFiles(sDir) returns an array of string. The original method list_all(), takes all of them. I want mine to just take the first 10, or if there's less than 10 then all of them. But i've added four lines of code and used a list, to do it. My amended method is list_some()

It's far from a disaster, but I'm sure there's a better way though I'm not sure.

using System;
using System.Collections.Generic;
using System.IO;

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

static void list_all()
{
string sDir = "c:\\windows";

foreach (string f in Directory.GetFiles(sDir))
{
Console.WriteLine(f);
}

}

static void list_some()
{
string sDir="c:\\windows";

List<string> lstsomefiles = new List<string>();
int count = Directory.GetFiles(sDir).Length;
string[] strs_fileslst = Directory.GetFiles(sDir);

for (int i = 0; i < count && i < 10; i++) lstsomefiles.Add(strs_fileslst[i]);

foreach (string f in lstsomefiles)
{

Console.WriteLine(f);
}

}

}
}
}

• Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. – Sᴀᴍ Onᴇᴌᴀ May 14 '20 at 22:10
• @SᴀᴍOnᴇᴌᴀ better? – barlop May 14 '20 at 22:13
• Maybe I should have linked to this help center page: How do I ask a good question?... it is an improvement but hopefully, after reading that page, you understand what would make it even better... – Sᴀᴍ Onᴇᴌᴀ May 14 '20 at 22:17
• Welcome to Code Review, where we review actual working code from your project. The function names blah_all and blah_some as well as the variable name lstsomefiles seem more hypothetical than real code. In C# this code would also be in a class, and that I don't see. As Sam indicated above, please read the Asking section of the Code Review Guidelines. – pacmaninbw May 14 '20 at 22:29
• @pacmaninbw i've now included the class and improved the function names. I made a simple example to give a simple easily demonstrable code that gets to the crux of the question I am asking. Maybe my question is more for stackoverflow if you don't want simple easily demonstrable code? – barlop May 14 '20 at 22:37

You can utilize the .Take() method in LINQ which:

Returns a specified number of contiguous elements from the start of a sequence

Using this, your code would look as follows:

string sDir = "c:\\windows";

foreach (string f in Directory.GetFiles(sDir).Take(10))
{
Console.WriteLine(f);
}

• wow that's beautiful – barlop May 14 '20 at 23:31
• I think, you can optimize a bit by using Directory.EnumerateFiles(path).Take(10). EnumerateFiles does not materialize all the file names in an array before returning. It operates directly on an Enumerator. – user73941 May 15 '20 at 6:38
• @barlop: In general, whenever you iterate over a list, 9 times out of 10 there's a cleaner LINQ approach. It's really worth having a look at what LINQ offers since it will dramatically improve code readability and prevent you from having to juggle list manipulation yourself. – Flater May 15 '20 at 9:42
• As a second choice, supposing one didn't use LINQ, was what I wrote in my amendment unnecessarily laborious? what is a good way? – barlop May 15 '20 at 12:38
• To support @HenrikHansen's point: "The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient." – 200_success May 15 '20 at 15:21