James
James

Reputation: 21

Avoid max char limit c#

To exceed the max path limit in c# you apparently need to concatenate your drive path with @"\\?\" at the beginning of it. If I do this then I get a drive path with the following at the front

\\\\?\\\\\\server\\share\\...

Now if I look for the file/folder it will fail because of illegal charachters in the path (I assume the ?) so how can I adopt the approach outlined on Microsoft's website (http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx) correctly?

foreach (string filePath in Directory.GetFiles(folder))
{
    String s = @"\\?\" + filePath;

    if (filePath.Length > 255)
    {
        Console.WriteLine(filePath);
    }

    if (File.Exists(filePath))
    {
        FileInfo finfo = new FileInfo(s);
        folderSize += finfo.Length;
    }
}

foreach (string dir in Directory.GetDirectories(folder))
    folderSize += GetDirectorySize(dir);

Upvotes: 2

Views: 1934

Answers (2)

Dariusz Wasacz
Dariusz Wasacz

Reputation: 1016

Standard System.IO doesn't support path longer than 260 characters.

But it seems there's a library with extended-length path support: http://alphafs.codeplex.com/

I personally haven't tried it so far.

Upvotes: 2

Davide Piras
Davide Piras

Reputation: 44605

I don't think you should add all those slashes before the path, I think you did not understand everything what was written in MSDN here:

Maximum Path Length Limitation

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

Note: File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\?\" prefix as detailed in the following sections.

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the "\?\" prefix. For example, "\?\D:\very long path".

Note: The maximum path of 32,767 characters is approximate, because the "\?\" prefix may be expanded to a longer string by the system at run time, and this expansion applies to the total length.

as you can read there,:

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters.

this is the key for your issue, if you need to create or access to a path longer than ~260 chars you should use specific Windows APIs.

Upvotes: 1

Related Questions