-2

I like to read the contents of the folder "files" which is in solution not in local machine . (which contains multiple text files. I want to get all the file names in the folder "files") .

  • 1
    Maybe look into the `File` class, `DirectoryInfo` class, `System.IO` namespace – Callum Linington Jun 24 '15 at 07:33
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](http://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Stefan Over Jun 24 '15 at 07:33
  • Have you tried to search for it? "c# get all the file names in the folder" – Tim Schmelter Jun 24 '15 at 07:35
  • I added a folder in solution (textFiles) , which contains some .txt files. Now i want to iterate through that specific folder. – Mohammad Shakir Jun 24 '15 at 10:15

1 Answers1

1

Please see MSDN: How To Enumerate Files . Please be aware that EnumerateFiles returns a hot Enumerable. If you iterate two times, it will read the files two times. You can create a list of these files.

    string dirPath = @"c:\files";

    foreach (var file in Directory.EnumerateFiles(dirPath))
    {
        Console.WriteLine(file)
    }
    Console.WriteLine("{0} directories found.",  dirs.Count);

If you really only nead the names you could also look here MSDN Directory.GetFiles

 string [] fileEntries = Directory.GetFiles(targetDirectory);

foreach(string fileName in fileEntries)
{
    Console.WriteLine(fileName);
}

If you want to do more then just display the names (e.g. processing the files) then EnumerateFiles would be better as you have only at a time in memory.

Boas Enkler
  • 12,264
  • 16
  • 69
  • 143