Get File extension name in CSharp

Description

The following code shows how to get File extension name.

Example


         //from w  ww .j a v  a 2 s.co m

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class MainClass
{
    static void Main(string[] args)
    {
        string[] files = Directory.GetFiles(@"c:\");
        foreach (string filename in files)
        {
            FileInfo file = new FileInfo(filename);
            Console.WriteLine(file.Extension);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip