FileInfo - HasExtension
Determines whether a file name extension.
public static void Main() { string FileName = "folder\\subfolder\\test.txt"; // C# Extension Method: FileInfo - HasExtension if(FileName.ToFileInfo().HasExtension()) { Console.WriteLine("File has extension."); } else { Console.WriteLine("File has no extension."); } }
View Source
using System; using System.IO; public static partial class Extensions { /// <summary> /// Determines whether a @this includes a file name extension. /// </summary> /// <param name="this">The @this to search for an extension.</param> /// <returns> /// true if the characters that follow the last directory separator (\\ or /) or volume separator (:) in the /// @this include a period (.) followed by one or more characters; otherwise, false. /// </returns> /// ### /// <exception cref="T:System.ArgumentException"> /// <paramref name="this" /> contains one or more of the invalid /// characters defined in /// <see /// cref="M:System.IO.Path.GetInvalidPathChars" /> /// . /// </exception> public static Boolean HasExtension(this FileInfo @this) { return Path.HasExtension(@this.FullName); } }