Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
SuggestionRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Richard Deeming29-Jan-24 3:56
mveRichard Deeming29-Jan-24 3:56 
QuestionHow to customize the default .Net Framework Validation Error Messages Pin
Fokwa Divine11-Jan-24 3:30
Fokwa Divine11-Jan-24 3:30 
AnswerRe: How to customize the default .Net Framework Validation Error Messages Pin
Pete O'Hanlon11-Jan-24 3:46
mvePete O'Hanlon11-Jan-24 3:46 
GeneralRe: How to customize the default .Net Framework Validation Error Messages Pin
Fokwa Divine11-Jan-24 4:17
Fokwa Divine11-Jan-24 4:17 
GeneralRe: How to customize the default .Net Framework Validation Error Messages Pin
Pete O'Hanlon11-Jan-24 18:56
mvePete O'Hanlon11-Jan-24 18:56 
Questiondisplay only first 10 files from each directory Pin
picasso29-Jan-24 11:59
picasso29-Jan-24 11:59 
AnswerRe: display only first 10 files from each directory Pin
jschell9-Jan-24 12:39
jschell9-Jan-24 12:39 
AnswerRe: display only first 10 files from each directory Pin
OriginalGriff9-Jan-24 19:54
mveOriginalGriff9-Jan-24 19:54 
There are several ways to do that: the most basic is to add a count to your loop:
C#
int showCount = 10;
foreach (string f in Directory.GetFiles(directory))
{
      Console.WriteLine(lvlSeperator + Path.GetFileName(f));
      if (--showCount <= 0) break;
}
A better way is to use the array as an Enumerable and only process ten items:
C#
foreach (string f in Directory.GetFiles(directory).Take(10))
{
      Console.WriteLine(lvlSeperator + Path.GetFileName(f));
}
But ... the order in which Directory.GetFiles "delivers" files is not defined by the method definition:
The order of the returned file names is not guaranteed; use the Sort method if a specific sort order is required.
So you need first decide what are "the first ten" and sort them according to that decision: name order? Create date? Modification date? Size? Ascending or descending?
We can't make that decision for you: we don't know your app!

You should also be aware that Directory.GetFiles has a number of overloads, one of which allows you to return all files in all subdirectories in a single call: Directory.GetFiles Method Overload 3[^] which may improve your app performance with large directory structures.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

AnswerRe: display only first 10 files from each directory Pin
Richard Deeming9-Jan-24 23:44
mveRichard Deeming9-Jan-24 23:44 
GeneralRe: display only first 10 files from each directory Pin
OriginalGriff10-Jan-24 23:27
mveOriginalGriff10-Jan-24 23:27 
GeneralRe: display only first 10 files from each directory Pin
Richard Deeming11-Jan-24 0:45
mveRichard Deeming11-Jan-24 0:45 
QuestionCompatibity Pin
PedroSini9-Jan-24 5:06
PedroSini9-Jan-24 5:06 
AnswerRe: Compatibity Pin
Pete O'Hanlon9-Jan-24 5:15
mvePete O'Hanlon9-Jan-24 5:15 
QuestionGet List Of Physical & Logical Drives Pin
Kevin Marois4-Jan-24 22:15
professionalKevin Marois4-Jan-24 22:15 
AnswerRe: Get List Of Physical & Logical Drives Pin
Richard Deeming4-Jan-24 22:50
mveRichard Deeming4-Jan-24 22:50 
GeneralRe: Get List Of Physical & Logical Drives Pin
Kevin Marois5-Jan-24 7:32
professionalKevin Marois5-Jan-24 7:32 
QuestionI need to convert a string to a float.... Pin
glennPattonWork33-Jan-24 2:15
professionalglennPattonWork33-Jan-24 2:15 
AnswerRe: I need to convert a string to a float.... Pin
Ron Nicholson3-Jan-24 2:23
professionalRon Nicholson3-Jan-24 2:23 
GeneralRe: I need to convert a string to a float.... Pin
glennPattonWork33-Jan-24 2:29
professionalglennPattonWork33-Jan-24 2:29 
GeneralRe: I need to convert a string to a float.... Pin
OriginalGriff3-Jan-24 5:05
mveOriginalGriff3-Jan-24 5:05 
AnswerRe: I need to convert a string to a float.... Pin
Richard Deeming3-Jan-24 2:30
mveRichard Deeming3-Jan-24 2:30 
GeneralRe: I need to convert a string to a float.... Pin
glennPattonWork33-Jan-24 2:51
professionalglennPattonWork33-Jan-24 2:51 
GeneralToList() or not to List. Pin
Gerry Schmitz16-Dec-23 10:10
mveGerry Schmitz16-Dec-23 10:10 
GeneralRe: ToList() or not to List. Pin
Dave Kreskowiak16-Dec-23 13:44
mveDave Kreskowiak16-Dec-23 13:44 
GeneralRe: ToList() or not to List. Pin
Gerry Schmitz17-Dec-23 5:27
mveGerry Schmitz17-Dec-23 5:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.