16

I need to get the first file name from a folder. How can I get this in C#?

The code below returns all the file names:

DirectoryInfo di = new DirectoryInfo(imgfolderPath); foreach (FileInfo fi in di.GetFiles()) { if (fi.Name != "." && fi.Name != ".." && fi.Name != "Thumbs.db") { string fileName = fi.Name; string fullFileName = fileName.Substring(0, fileName.Length - 4); MessageBox.Show(fullFileName); } } 

I need the first file name.

4
  • 7
    If you accept some of the answers to your other questions, people are going to be more likely to help you. Commented May 26, 2010 at 11:33
  • A better title might be something like: "How to find a filename that matches a predicate from a folder in C#" Commented May 26, 2010 at 11:37
  • 1
    What version of .NET are you using? Commented May 26, 2010 at 11:41
  • 3
    What qualifies as the "first" file? The oldest file? The file with the "lowest" alphabetical name? Commented May 26, 2010 at 12:10

5 Answers 5

35

There's a few ways you could do this:

  • You could add a break statement after handling the first file. This will exit the foreach loop.

  • DirectoryInfo.GetFiles returns an array so you can assign it to a variable and scan through the elements until you find a suitable element.

  • Or if you are using .NET 3.5 you could look at the FirstOrDefault method with a predicate.

Here's some code:

string firstFileName = di.GetFiles() .Select(fi => fi.Name) .FirstOrDefault(name => name != "Thumbs.db"); 
Sign up to request clarification or add additional context in comments.

2 Comments

oh thanks..but i was trying to declare array and blay bala.. anyway thanks
Given that GetFiles enumerates all files in the directory in order to create an array, it would be more efficient to use EnumerateFiles in this case. For more info, see msdn.microsoft.com/en-us/library/wz42302f(v=vs.110).aspx
9

If you are using .Net 4.0 you should do this instead...

var firstFileName = di.EnumerateFiles() .Select(f => f.Name) .FirstOrDefault(); 

... .GetFiles() creates an array and as such must scan all files. .EnumerateFiles() will return an IEnumerable<FileInfo> so it doesn't have to do as much work. You probably won't notice mush of a difference on a local hard drive with a small number of files. But a network share, thumb drive/memory card, or huge number of files would make this obvious.

3 Comments

What? If you are looking for a particular file add the search string. if you are just doing a blanket get first file then if that's thumbs.db then that's just it. Either way .EnumerateFiles(...) will only return files one at a time which is really helpfull in directories with tons of files and folders. msdn.microsoft.com/en-us/library/…
Hi. You are right. I using this solution => stackoverflow.com/a/2912435/1395101 but not interesting ?! because may be returned "*.any file extension" and i need code returned first only for example .jpg file . at the end i found this solution => stackoverflow.com/a/3152180/1395101
You wil still want to use .EnumerateFiles(...) if you are using .Net 4.0 or greater.
4
FileInfo fi = di.GetFiles()[0]; 

Notes:

  • The code throws an exception if there are no files.
  • "First" is ambiguous — do you mean any file, or the first one alphabetically? In the latter case, you may need to worry about stuff like case-sensitivity and locale-dependent sorting.

Comments

2

In reply to riad's comment to me:

In addition to abatischchev's solution:

var file = Directory.GetFiles(@"C:\TestFolder", "*.*") .FirstOrDefault(f => f != @"C:\TestFolder\Text1.txt"); 

I would add this to get the name only:

Console.WriteLine(file.Substring(file.LastIndexOf('\\') + 1)); 

Which generates the output Text2.txt (I have three text tiles in that folder called Text1.txt, Text2.txt and text3.txt.

Comments

1
using System.IO; using System.Linq; var firstFile = Path.GetFileName(Directory.GetFiles(@"c:\dir", "*.*") .FirstOrDefault(f => !String.Equals( Path.GetFileName(f), "Thumbs.db", StringComparison.InvariantCultureIgnoreCase))); 

10 Comments

Wouldn't you have to supply the full path to the lambda to get the Where clause to work? Just curious? :)
Note: You don't need a separate call to Where - FirstOrDefault can accept a predicate.
True, @Mark Byers, and I don't think the above code does what is required or am I missunderstanding something? Directory.GetFiles() returns a string containing the full paths as the file names, so you would need to take that into account in the predicate as well.
@Mark: GetFiles return string[].
Yes daft you rite.its return the full path of .I just need the first file name.Could you pls guide me how can i get the only first file name?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.