peteraritchie / LongPath

drop-in library to support long paths in .NET
GNU Lesser General Public License v3.0
112 stars 43 forks source link

Error with a really long and strange filename. #98

Open alkampfergit opened 1 year ago

alkampfergit commented 1 year ago

If I'm trying to save a file with a really strange name I got that error

System.ArgumentException : The filename, directory name, or volume label syntax is incorrect.

Parameter name: path

I can replicate the bug changing each letter to an X,

"xxxxxx x xxx xxxxx xxxxxxxxxxx xxxxx xxx xxxxxxxx xxxxxxxxx xxxxx xx xxxxxxxxxxx xxxxx . xxxxx xxxxxxx xx xxxx xxxxxxx xxxxx xxxxx, xx xxxxxxxxxx xxxx xxxxxxr xxxxxx xxxx xx xxxxxxx 2 xxxxxxxxs xxx xxxx xxxxx xxxxxxxxxx xxx xxx xxxx xxxxxxxxxxxx xxxxx..eml"

Just create a console app with this code and you got a crash.

    internal class Program
    {
        static void Main(string[] args)
        {
            var tempPath = Path.GetTempPath();
            var fileName = Path.Combine(tempPath, "xxxxxx x xxx xxxxx xxxxxxxxxxx xxxxx xxx xxxxxxxx xxxxxxxxx xxxxx xx xxxxxxxxxxx xxxxx . xxxxx xxxxxxx xx xxxx xxxxxxx xxxxx xxxxx, xx xxxxxxxxxx xxxx xxxxxxr xxxxxx xxxx xx xxxxxxx 2 xxxxxxxxs xxx xxxx xxxxx xxxxxxxxxx xxx xxx xxxx xxxxxxxxxxxx xxxxx..eml");

            Pri.LongPath.File.WriteAllText(fileName, "Hello world");
        }
    }
alkampfergit commented 1 year ago

Actually the problem is a really too long filename, i'd like to have simply a better error message telling that the filename is simply too long.

peteraritchie commented 1 year ago

What LongPath does is to sneak UNC into the mix to support up to 32k characters per path to avoid the PathTooLongException. UNC still has limitations on each component (filename, directory name, etc.). Depending on the volume, the component limit is 255. The filename in your example is 256 characters. When a UNC component is longer that 255 Windows responds with an error 123 (ERROR_INVALID_NAME). When LongPath encounters that error, it translates it to ArgumentException. The same error in .NET would cause an IOException with the same message and HResult. The fix would be to correct that use of ArgumentException to be compatible with .NET

alkampfergit commented 1 year ago

I've opened the bug because we found this error on a log with a filename full of weird characters, and we were absolutely sure that one of the char was the issue, then we find that is a simple filename too long. :).

peteraritchie commented 1 year ago

I've opened the bug because we found this error on a log with a filename full of weird characters, and we were absolutely sure that one of the char was the issue, then we find that is a simple filename too long. :).

Amusingly, a file name can only be up to 255 characters in a UNC filename. If you don't use UNC, you can get a 260 character file name in the root of the drive :)

alkampfergit commented 1 year ago

Windows FileSystems are always amazing :D.