Gets the input last extension from the given input file name. - CSharp System.IO

CSharp examples for System.IO:File Name

Description

Gets the input last extension from the given input file name.

Demo Code

//     TelerikAcademy.com. All rights reserved.
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*w w  w. j  av a2s.c  om*/

public class Main{
        /// <summary>
        /// Gets the input last extension from the given input file name.
        /// </summary>
        /// <param name="fileName">Input file name string.</param>
        /// <returns>Returns the last extension in a lowercase.</returns>
        public static string GetFileExtension(this string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return string.Empty;
            }

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

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

Related Tutorials