Using ExtendedFileInfo Project to avoid a System.IO.PathTooLongException.

The ExtendedFileInfo Project is a libary that you can use if you have path information longer than 248 chars and file and path information longer than 260 chars. When you search for a solution to the
System.IO.PathTooLongException: “The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.”
the ExtendedFileInfo is what you are searching for. I had the problem at a project where the path where xml configuration files where saved and the name of the xml file exceeded 260 chars. The use of that files where covered over the whole project so i had to find a global solution that uses no FileInfo class if the path and the filename where longer as 260 chars. To accomplish that i implemented the ExtendetFileInfo library and i put it to codeplex. It works the following way, if i access a file the library checks the path and if the path is too long it uses the windows api functions to access the file an no PathTooLongException is thrown. I implemented a IFileInfo interface that is used to hide the calls to the windows api or to a FileInfo object.

ExtendedFileInfo

ExtendedFileInfo class diagram

Here you see a test call that uses a short and a long path to a specific file:

[TestClass]
public class ExtendedFileInfoTest
{
    [TestMethod]
    public void ExtendedFileInfoTestShortFilePath()
    {
        var shortFilePath = @"C:\FilePath\ShortPath\Textdocument.txt";
        var extendedFileInfo = FileInfoFactory.Create(shortFilePath);
        using(var fileStream = extendedFileInfo.Open(FileMode.Open, FileAccess.Read))
        {
            var buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
        }
    }

    [TestMethod]
    public void ExtendedFileInfoTestLongFilePath()
    {
        var longFilePath =
            @"C:\FilePath\" + 
@"Looooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + 
@"oooooooooooooooooooooooooooooooooooooooooooooooooPath\" +
@"Looooooooooooooooooooooooooooooooooooooooooooooooooooo" + 
@"ooooooooooooooooooooooooooooooooooooooooooooooooooooog\" + 
"Textdocument.txt";
        var extendedFileInfo = FileInfoFactory.Create(longFilePath);
        using (var fileStream = extendedFileInfo.Open(FileMode.Open, FileAccess.Read))
        {
            var buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
        }
    }
}

Extended IFileInfo interface has the following methodes and properties:

public interface IFileInfo
{
    bool Exists();
    bool IsReadOnly { get; }        
    long Length { get; }        
    string Name { get; }
    FileStream Open(FileMode fileMode, FileAccess fileAccess);
    IFileInfo CopyTo(string destFileName);        
    IFileInfo CopyTo(string destFileName, bool overwrite);
    void MoveTo(string destFileName);
    void Delete();
    void SetAttributes(FileAttributes fileAttributes);
    FileAttributes GetAttributes();
    DateTime CreationTime { get; }                
    string Extension { get; }
    string FullName { get; }
}

That means you can use all that methodes and properties like you would use it with a .net FileInfo object. So when i had that problem in my project it took me a lot of time to find the correct winapi calls and use them the correct way. So i thought if someone has the same problem it would be nice to just include a dll and the call to the FileInfo object works again.

Advertisements

2 Comments on “Using ExtendedFileInfo Project to avoid a System.IO.PathTooLongException.”

  1. Anders says:

    Thanks for sharing , very good extension saved me a lot of headache


If you have a note or a question please write a comment.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s