C# PathTooLongException
last modified November 6, 2026
In this article we show when the PathTooLongException is thrown
in C# and how to avoid it.
PathTooLongException from the System.IO namespace is
thrown when a path or one of its components is longer than the operating
system allows. It derives from IOException. The limits depend on
the system:
- Linux: 255 bytes per file or directory name, 4096 bytes for the whole path
- macOS: 255 bytes per name, 1024 bytes for the whole path
- Windows: 255 characters per name; the whole path was limited to 260
characters (
MAX_PATH) in the classic Windows API
On Windows, .NET Core and later versions support paths longer than 260
characters: .NET adds the \\?\ prefix internally, so the code
does not have to. Other programs, such as some older tools, may still fail
with such paths. In .NET Framework, the 260-character limit applies unless
long path support is turned on.
The examples create a demo directory in the working directory and
delete it at the end. The outputs come from Linux.
Basic example
The first example creates a file whose name is longer than 255 characters.
Directory.CreateDirectory("demo");
string name = new string('a', 256) + ".txt";
try
{
File.WriteAllText(Path.Combine("demo", name), "data");
}
catch (PathTooLongException e)
{
string message = e.Message
.Replace(Environment.CurrentDirectory, ".")
.Replace(name, $"aaa...({name.Length} chars)");
Console.WriteLine(message);
Console.WriteLine($"HResult: 0x{e.HResult:X8}");
Console.WriteLine($"base type: {e.GetType().BaseType}");
}
Directory.Delete("demo", true);
We catch the exception and print its message, error code, and base type.
.Replace(name, $"aaa...({name.Length} chars)");
The message contains the whole path, so we replace the working directory and the long name to keep the output readable.
Console.WriteLine($"HResult: 0x{e.HResult:X8}");
The error code 0x800700CE is the Windows code
ERROR_FILENAME_EXCED_RANGE. .NET uses it on all platforms for
this exception.
$ dotnet run The path './demo/aaa...(260 chars)' is too long, or a component of the specified path is too long. HResult: 0x800700CE base type: System.IO.IOException
Bytes, not characters
On Linux and macOS, the limit for a name is measured in bytes of its UTF-8 encoding. A name with non-ASCII characters reaches the limit with fewer characters.
Directory.CreateDirectory("demo");
Try(new string('a', 255));
Try(new string('\u010D', 127));
Try(new string('\u010D', 128));
Directory.Delete("demo", true);
static void Try(string name)
{
int bytes = System.Text.Encoding.UTF8.GetByteCount(name);
try
{
File.WriteAllText(Path.Combine("demo", name), "");
Console.WriteLine($"{name.Length} chars, {bytes} bytes: created");
}
catch (PathTooLongException)
{
Console.WriteLine($"{name.Length} chars, {bytes} bytes: PathTooLongException");
}
}
We create files with names of 255 ASCII characters and of 127 and 128 copies of the letter č.
int bytes = System.Text.Encoding.UTF8.GetByteCount(name);
GetByteCount returns the length of the UTF-8 encoding. The letter
č, written as a Unicode escape in the code, takes two bytes.
The name of 128 characters is 256 bytes long and fails, while the ASCII name of 255 characters succeeds. On Windows, the limit counts UTF-16 characters, so all three names would work.
$ dotnet run 255 chars, 255 bytes: created 127 chars, 254 bytes: created 128 chars, 256 bytes: PathTooLongException
The total path length
The whole path has its own limit, which is reached with deep directory trees.
string path = "demo";
for (int i = 0; i < 20; i++)
{
path = Path.Combine(path, new string('d', 250));
}
string full = Path.GetFullPath(path);
Console.WriteLine($"full path length: {full.Length}");
try
{
Directory.CreateDirectory(path);
}
catch (PathTooLongException)
{
Console.WriteLine("CreateDirectory: PathTooLongException");
}
Console.WriteLine($"demo exists: {Directory.Exists("demo")}");
string shorter = string.Join(Path.DirectorySeparatorChar, path.Split(Path.DirectorySeparatorChar)[..15]);
Directory.CreateDirectory(shorter);
Console.WriteLine($"created a path of {Path.GetFullPath(shorter).Length} chars");
Directory.Delete("demo", true);
We build a path of twenty nested directories with 250-character names and try to create it.
string full = Path.GetFullPath(path);
GetFullPath works with strings only, so it accepts the long path
without an error.
Directory.CreateDirectory(path);
CreateDirectory checks the full path before creating anything,
so the exception is thrown and no directory is created, not even
demo.
string shorter = string.Join(Path.DirectorySeparatorChar, path.Split(Path.DirectorySeparatorChar)[..15]);
With fifteen components, the path fits below the limit of 4096 bytes and the tree is created.
$ dotnet run full path length: 5163 CreateDirectory: PathTooLongException demo exists: False created a path of 3657 chars
Validating paths
When paths come from configuration or user input, they can be checked before any file operation.
using System.Text;
const int maxNameBytes = 255;
const int maxPathBytes = 4095;
string[] paths =
[
Path.Combine("data", "report.txt"),
Path.Combine("data", new string('x', 300) + ".txt"),
string.Join('/', Enumerable.Repeat(new string('y', 200), 25)),
];
foreach (string p in paths)
{
Console.WriteLine($"{p[..Math.Min(p.Length, 20)],-20} {Check(p)}");
}
static string Check(string path)
{
string full = Path.GetFullPath(path);
if (Encoding.UTF8.GetByteCount(full) > maxPathBytes)
{
return "path too long";
}
foreach (string part in full.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries))
{
if (Encoding.UTF8.GetByteCount(part) > maxNameBytes)
{
return $"name too long: {part.Length} chars";
}
}
return "ok";
}
The Check method tests the length of the full path and of each
of its names against the Linux limits.
const int maxNameBytes = 255; const int maxPathBytes = 4095;
The path limit of 4096 bytes includes the terminating null byte, so a path may have at most 4095 bytes.
foreach (string part in full.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries))
We split the full path into its names and measure each one. Such a check reports a clear error early, but it cannot replace exception handling: other file systems, such as network shares, may have lower limits.
$ dotnet run data/report.txt ok data/xxxxxxxxxxxxxxx name too long: 304 chars yyyyyyyyyyyyyyyyyyyy path too long
Shortening generated names
File names derived from titles, URLs, or other data can be longer than the limit. A helper can shorten them while keeping them unique.
using System.Security.Cryptography;
using System.Text;
Directory.CreateDirectory("demo");
string title = string.Concat(Enumerable.Repeat("Annual report of the regional office ", 10));
string name = ShortenFileName(title.Trim() + ".pdf", 100);
Console.WriteLine($"original: {title.Length + 4} chars");
Console.WriteLine($"shortened: {name.Length} chars");
Console.WriteLine(name);
File.WriteAllText(Path.Combine("demo", name), "");
Console.WriteLine($"created: {File.Exists(Path.Combine("demo", name))}");
Directory.Delete("demo", true);
static string ShortenFileName(string name, int maxBytes)
{
if (Encoding.UTF8.GetByteCount(name) <= maxBytes)
{
return name;
}
string ext = Path.GetExtension(name);
string stem = Path.GetFileNameWithoutExtension(name);
string hash = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(name)))[..8];
string suffix = $"-{hash}{ext}";
var sb = new StringBuilder();
foreach (char c in stem)
{
if (Encoding.UTF8.GetByteCount(sb.ToString() + c + suffix) > maxBytes) break;
sb.Append(c);
}
return sb.ToString().TrimEnd() + suffix;
}
We create a file name from a long title and limit it to 100 bytes.
string hash = Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(name)))[..8];
string suffix = $"-{hash}{ext}";
Two long titles with the same beginning would be shortened to the same name. A short hash of the full name, added before the extension, keeps the names different.
if (Encoding.UTF8.GetByteCount(sb.ToString() + c + suffix) > maxBytes) break;
We add characters while the result fits. Working with whole characters instead of cutting the byte array ensures that a multi-byte character is never split.
$ dotnet run original: 374 chars shortened: 100 chars Annual report of the regional office Annual report of the regional office Annual report-5c490141.pdf created: True
Handling the exception
A program that saves many files can skip the names that are too long and continue.
Directory.CreateDirectory("demo");
string[] names = ["notes.txt", new string('n', 300) + ".txt", "todo.txt"];
int saved = 0;
foreach (string name in names)
{
string path = Path.Combine("demo", name);
try
{
File.WriteAllText(path, "content");
saved++;
}
catch (PathTooLongException)
{
Console.WriteLine($"skipped a name of {name.Length} characters");
}
catch (IOException e)
{
Console.WriteLine($"I/O error: {e.Message}");
}
}
Console.WriteLine($"saved {saved} of {names.Length} files");
Directory.Delete("demo", true);
We save three files, one of which has a name of 304 characters.
catch (PathTooLongException)
{
Console.WriteLine($"skipped a name of {name.Length} characters");
}
catch (IOException e)
PathTooLongException is caught before the more general
IOException. The other two files are saved.
$ dotnet run skipped a name of 304 characters saved 2 of 3 files
Source
In this article we have worked with the PathTooLongException in
C#.
Author
List all C# tutorials.