Get File Extension - CSharp System.IO

CSharp examples for System.IO:File Name

Description

Get File Extension

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.IO;/*from  w  w w. ja va 2 s. c o m*/
using System.Collections.Generic;
using System;

public class Main{
        public static string GetFileExtension(this string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return string.Empty;
            }

            string[] fileParts = fileName.Split(new[] { "." }, StringSplitOptions.None);
            if (fileParts.Length == 1 || string.IsNullOrEmpty(fileParts.Last()))
            {
                return string.Empty;
            }

            return fileParts.Last().Trim().ToLower();
        }
}

Related Tutorials