Within a project we create a path to a file by using some path traveral (\..) to find the correct location of a file. The concrete full path to this file is always below the 260 characters mark. Now we had an exception if the traversal path is exactly 260 characters, but not if it is longer.
To show you the problem i made a little test that throws an exception i can’t explain, any ideas?
string root = "c:\\fold";
string subfolder = "\\aFolder";
string traversal = "\\..";
// creates the string ""c:\\fold\\aFolder\\..\\aFolder\\.." ... with length 249
var pathShorterThen260 = root + Enumerable.Repeat(subfolder + traversal, 22).Aggregate((first, second) => first + second);
// Works without any problem and returns "c:\fold"
var exactPath1 = Path.GetFullPath(pathShorterThen260);
// creates the string ""c:\\fold\\aFolder\\..\\aFolder\\.." ... with length 271
var pathGreaterThen260 = root + Enumerable.Repeat(subfolder + traversal, 24).Aggregate((first, second) => first + second);
// Works without any problem and returns "c:\fold"
var exactPath2 = Path.GetFullPath(pathGreaterThen260);
// creates the string ""c:\\fold\\aFolder\\..\\aFolder\\.." ... with length 260
var pathEqualTo260 = root + Enumerable.Repeat(subfolder + traversal, 23).Aggregate((first, second) => first + second);
// Throws PathTooLongException, why?
var exactPath3 = Path.GetFullPath(pathEqualTo260);
Microsoft has confirmed the bug and it is closed in .Net 4.5.
For more information take a look at Microsoft Connect.