Home C# Listing files in a directory
Listing files in a directory PDF Print E-mail
Written by Siva   
Sunday, 31 October 2010 02:17

// Use the System.IO reference! - Siva

// Best practice you may use Visual Studio 2010 with .Net Framework 4.0

// but .Net Framework 4.0 is new; use standard Framework what your corporation uses..

=== Program that gets files in directories (C#) ===

using System;
using System.IO;

class Program
{
    static void Main()
    {
        // Put all file names in root directory into array.
        string[] array1 = Directory.GetFiles(@"C:\");

        // Put all txt files in root directory into array.
        string[] array2 = Directory.GetFiles(@"C:\", "*.BIN"); // <-- Case-insensitive

        // Display all files.
        Console.WriteLine("--- Files: ---");
        foreach (string name in array1)
        {
            Console.WriteLine(name);
        }

        // Display all BIN files.
        Console.WriteLine("--- BIN Files: ---");
        foreach (string name in array2)
        {
            Console.WriteLine(name);
        }
    }
}

=== Output of the program ===

--- Files: ---
    (All files in your C:\ folder)
--- BIN Files: ---
    (All .bin, .BIN files in your C:\ folder)

 
 

Advertisement

Featured Links:
HREmail.com
Looking for job? Try HREmail.com
Want to have CMS based Websites?
Try siliconwebcreators.com to CMS based websites.
VM Solutions Inc
For best VM solutions..
Digital Procedure Inc.
Looking for Oracle /SAP /Weblogic Consulting services? visit digitalprocedure.com