If you have used the System.IO namespace before, then the LongPathFile and LongPathDirectory classes will feel very familiar. These classes, where ever possible, attempt to replicate the existing API and behavior of their short path equivalents; the File and Directory classes.

Note:

Unlike the underlying Win32 APIs, the LongPathDirectory and LongPathFile classes treat short and long directory and file names exactly the same. Because of this, there is no need to prefix long paths with the Win32 specifier \\?\ - doing so will result in an ArgumentException being thrown for passing in an invalid path.

Known Limitations:
  • While path lengths have been increased to 32,000 characters, individual components within paths (for example, Windows in C:\Windows\System32) remain limited to 255 characters. Passing in a path with a component that exceeds 255 characters, will result in an ArgumentException being thrown.

I want to:

Determine if a file exists
Read from a file
Write to a file
Delete a file
Rename or move a file
Copy a file
Determine if a directory exists
Create or delete a directory
Enumerate files and directories

Common File Tasks

Determining if a file exists

The following example determines whether a file named C:\Temp\TextFile.txt exists and writes the result to the console.

string tempPath = @"C:\Temp\TextFile.txt";

if (LongPathFile.Exists(tempPath)) {

    Console.WriteLine(tempPath + " exists");
}
else {

    Console.WriteLine(tempPath + " does not exist");
}
Note: LongPathFile.Exists will return false if any error occurs while trying to determine if the specified file exists. This includes situations that would normally result in thrown exceptions including (but not limited to); passing in a file name with invalid or too many characters, an I/O error such as a failing or missing disk, or if the caller does not have Windows permissions to to read the file.

Reading from a file

The following example opens a existing file named C:\Temp\TextFile.txt and writes its contents to the console.

using (FileStream stream = LongPathFile.Open(@"C:\Temp\TextFile.txt", FileMode.Open, FileAccess.Read)) {

    using (StreamReader reader = new StreamReader(stream)) {

        string line;
        while ((line = reader.ReadLine()) != null) {
                        
            Console.WriteLine(line);
        }
    }
}

Writing to a file

The following example creates a new file named C:\Temp\TextFile.txt, overwriting it if it already exists, and writes some text to it.

using (FileStream stream = LongPathFile.Open(@"C:\Temp\TextFile.txt", FileMode.Create, FileAccess.Write)) {

    using (StreamWriter writer = new StreamWriter(stream)) {

        writer.Write("Hello World!");
    }
}

Deleting a file

The following example deletes a file named C:\Temp\TextFile.txt.

LongPathFile.Delete(@"C:\Temp\TextFile.txt");
Note: Similar to File.Delete, if the specified file is read-only, an IOException is thrown.

Renaming or moving a file

LongPathFile.Move can be used for both renaming a file (such as giving it a different extension), and for moving a file to a different directory or drive.

The following example renames a file named C:\Temp\TextFile.txt to C:\Temp\TextFile.bak.

LongPathFile.Move(@"C:\Temp\TextFile.txt", @"C:\Temp\TextFile.bak");

The following example moves a file named C:\Temp\TextFile.txt to a different directory and drive.

LongPathFile.Move(@"C:\Temp\TextFile.txt", @"D:\TextFile.txt");
Note: Similar to File.Move, if the destination path already exists an IOException is thrown.

Copying a file

The following examples copies a file named C:\Temp\TextFile.txt to a new file called C:\Temp\TextFile.bak, overwriting any existing file at the destination path.

LongPathFile.Copy(@"C:\Temp\TextFile.txt", @"C:\Temp\TextFile.bak", true);
Note: If the destination path already exists and false is passed for overwrite, an IOException is thrown.

Common Directory Tasks

Determining if a directory exists

The following example determines whether a directory named C:\Temp exists and writes the result to the console.

string tempPath = @"C:\Temp";

if (LongPathDirectory.Exists(tempPath)) {

    Console.WriteLine(tempPath + " exists");
}
else {

    Console.WriteLine(tempPath + " does not exist");
}
Note: LongPathDirectory.Exists will return false if any error occurs while trying to determine if the specified directory exists. This includes situations that would normally result in thrown exceptions including (but not limited to); passing in a directory name with invalid or too many characters, an I/O error such as a failing or missing disk, or if the caller does not have Windows permissions to to read the directory.

Creating and deleting directories

The following example creates a directory named C:\Temp.

LongPathDirectory.Create(@"C:\Temp");
Note: Unlike Directory.CreateDirectory, LongPathDirectory.Create only creates the last directory in the specified path. If the directory already exists, calling this method is a no-op.

The following example deletes a directory named C:\Temp.

LongPathDirectory.Delete(@"C:\Temp");
Note: Similar to the default overload of Directory.Delete, if the specified directory is not empty, an IOException is thrown.

Enumerating files and directories

Like the Directory class in .NET 4.0, LongPathDirectory has three methods, EnumerateFiles, EnumerateFiles, and EnumerateFileSystemEntries that return the contents of a directory lazily. This allows you to start handling individual files and directories without needing to wait for the file system to return all the contents of the directory.

The following example returns all the files and directories in the user's document directory, and writes them to the console.

string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

IEnumerable<string> paths = LongPathDirectory.EnumerateFileSystemEntries(documentsPath);

foreach (string path in paths) {

    Console.WriteLine(path);
}
The results from these methods can also be filtered by passing a search pattern. For example, the following code returns all the executables in the Windows directory and writes them to the console.

string windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows);

IEnumerable<string> executables = LongPathDirectory.EnumerateFiles(windowsPath, "*.exe");

foreach (string executable in executables) {

    Console.WriteLine(executable);
}

Last edited Apr 2, 2010 at 10:50 PM by davkean, version 33

Comments

becker34 Apr 3 at 5:57 PM 
I need this to be able to get file sizes without using System.IO's FileInfo. Maybe it is something to consider when adding new functionality?

derikogay Jan 28 at 10:19 AM 
You can use Long Path Tool for such problems, it works good I will say.

TomGroszko Jan 19, 2012 at 10:50 PM 
Both of these need a way to create one from a DirectoryInfo and FileInfo class.