PathTooLongException Class
The PathTooLongException
class represents an error that occurs when a path or fully qualified file name exceeds the system-defined maximum length.
Namespace
System.IO
Assembly
System.Runtime.dll
Inheritance
System.Object → System.Exception → System.SystemException → System.IO.IOException → System.IO.PathTooLongException
Constructor
public PathTooLongException();
public PathTooLongException(string message);
public PathTooLongException(string message, Exception innerException);
Properties
Message
– Retrieves the error message that explains the reason for the exception.InnerException
– The exception instance that caused the current exception.StackTrace
– A string representation of the immediate frames on the call stack.
Example
The following example demonstrates how to catch a PathTooLongException
when attempting to create a directory with an excessively long path.
using System;
using System.IO;
class Program
{
static void Main()
{
try
{
// Generate a path longer than 260 characters
string longPath = new string('a', 260);
Directory.CreateDirectory(longPath);
}
catch (PathTooLongException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}