Get list of file name available in the folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Latheesh
    New Member
    • Nov 2010
    • 1

    Get list of file name available in the folder

    hi guys,

    i want to get the name of the files available in the folder

    suppose i have D:\Test\Test.tx t

    i want to check whether it is test.txt file if another file comes other than that it should display me an error
  • Alex Bug
    New Member
    • Oct 2010
    • 3

    #2
    Get list file name can

    string[] files = Directory.GetFi les(Path);

    Check file File.Exist(file s[i]);

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Sorry, I didn't know this was a double post and responded to the wrong one. I'll report the other to be deleted :)

      Alex Bug has the correct answer. Here's some links to MSDN for more information on the Directory and File classes, which are super handy.

      Exposes static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.


      Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.

      Comment

      • Subin Ninan
        New Member
        • Sep 2010
        • 91

        #4
        Hi,
        to get list of files in a directory you can use Directory.GetFi les() method.

        Code:
        string[] files = Directory.GetFiles(path); //gets all files under specified directory.
        
        string[] txtFiles = Directory.GetFiles(path, "*.txt"); //gets all .txt files
        
        string[] allFiles = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories); //gets all .txt files from specified directory and its sub-folders.
        
        //To check whether file exist:
        
        if(File.Exists(@"D:\TEST\TEST.TXT"))
        {
        //exists
        }
        else
        {
        //FileNotFound
        }
        Last edited by Frinavale; Nov 26 '10, 09:55 PM. Reason: Fixed code tags.

        Comment

        Working...