C# Path HasExtension() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, Path.HasExtension() returns true when the file name part of a path has an extension, and false when it does not. For "report.txt" the answer is true; for "README" it is false.

It looks only at the last part of the path, so a dot in a folder name is ignored. A dot at the very end ("file.") is not counted as an extension, because nothing follows it. A null path gives false.

This is a static method of System.IO.Path. Call it as Path.HasExtension(...); there is no new Path(). Like most Path methods, it only works with text: it never opens, reads, or creates a file, and it works the same whether or not the path exists.

Syntax

Pass a path or a file name. The result is a bool.

C#
bool a = Path.HasExtension("report.txt");   // true
bool b = Path.HasExtension("README");       // false

if (!Path.HasExtension(name)) {
    name = Path.ChangeExtension(name, ".txt");
}
CallWhat it does
HasExtension("a.txt")Returns true.
HasExtension("README")Returns false.
HasExtension("file.")A dot with nothing after it. Returns false.
HasExtension(".gitignore")Returns true. The last dot starts the name.
HasExtension("/data.v1/log")The dot is in the folder part. Returns false.

HasExtension(path) is the same as Path.GetExtension(path).Length > 0, just easier to read. It tells you nothing about what the extension is.

Library declarations
C#
public static bool HasExtension(string? path);
public static bool HasExtension(ReadOnlySpan<char> path);

Parameters

ParameterDescription
pathA path or file name to test. It may be null, which gives false. The file does not need to exist.

Return Value

Return ValueDescription
booltrue if the last part of the path has a dot followed by at least one character; otherwise false.

C# Examples

Example 1 — Check Four File Names

Print the answer for a normal name, a name without a dot, a trailing dot, and a dot file.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] names = { "report.txt", "README", "file.", ".gitignore" };

        foreach (string name in names) {
            Console.WriteLine(name + ": " + Path.HasExtension(name));
        }
    }
}

How It Works

  1. A plain extension. report.txt has a dot with txt after it, so the answer is True.
  2. No dot. README has nothing to read as an extension, so the answer is False.
  3. A dot with nothing after it. file. ends at the dot, so no extension is found and the answer is False.
  4. A dot file. In .gitignore the last dot is the first character, and gitignore follows it, so the answer is True.

Example 2 — Add a Default Extension Only When It Is Missing

Combine HasExtension with ChangeExtension to fill in .txt for names that have none.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] inputs = { "notes", "todo.md", "/data.v1/log" };

        foreach (string input in inputs) {
            string name = input;
            if (!Path.HasExtension(name)) {
                name = Path.ChangeExtension(name, ".txt");
            }
            Console.WriteLine(name);
        }
    }
}

How It Works

  1. Three inputs. One has no extension, one has .md, and the third has a dot in the folder name only.
  2. Test first. !Path.HasExtension(name) is true for notes and /data.v1/log, so those two are changed.
  3. Existing extensions stay. todo.md already has one, so the if is skipped and the name is printed unchanged.
  4. The folder dot is ignored. Both methods look only at the last part, so /data.v1/log becomes /data.v1/log.txt and not /data.txt.

Example 3 — HasExtension Agrees With GetExtension

Prove that HasExtension is just a yes or no version of GetExtension, and see the null case.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string[] paths = { "a.txt", "a", "a.", "/x.y/z", "dir/b.tar.gz" };
        bool allAgree = true;

        foreach (string p in paths) {
            bool has = Path.HasExtension(p);
            bool longer = Path.GetExtension(p).Length > 0;
            if (has != longer) allAgree = false;
        }
        Console.WriteLine("All agree: " + allAgree);

        string missing = null;
        Console.WriteLine("null: " + Path.HasExtension(missing));
        Console.WriteLine("empty: " + Path.HasExtension(""));
    }
}

How It Works

  1. Five test paths. They cover a normal name, no dot, a trailing dot, a dot only in a folder, and two dots.
  2. Two ways to ask. For each path, HasExtension gives a bool, and GetExtension(p).Length > 0 gives the same answer from the string.
  3. No difference found. The flag stays true because the two answers always match, so All agree: True is printed.
  4. null and empty are safe. Both give False, with no exception, so you can test user input without a null check first.

Frequently Asked Questions

Call Path.HasExtension(name). It returns true for "report.txt" and false for "README".
No. "file." has a dot, but nothing follows it, so HasExtension returns false. Path.GetExtension also returns an empty string for it.
Write: if (!Path.HasExtension(name)) name = Path.ChangeExtension(name, ".txt"); A name that already has an extension is left alone.
Yes, they agree for every path. HasExtension just returns a bool, so the check is easier to read and there is no temporary string.
No. Only the last part of the path is checked. In "/data.v1/readme" the dot is before the last separator, so the answer is false.

Next: IsPathFullyQualified()

Check whether a path points to one exact place, independent of the working folder and drive.

IsPathFullyQualified() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful