0

DirectoryInfo.Create is throwing DirectoryNotFoundException when the path is longer than 260 characters. I understand that it is not possible to create the directory, but the method also supports PathTooLongException. Why isn't it thrown instead? When the path's lenght is >248 and <260 a PathTooLongException is thrown. Shouldn't both cases throw the same exception?

My code:

DirectoryInfo fullArchiveDirectoryInfo = new DirectoryInfo(fullArchiveDirectory);
fullArchiveDirectoryInfo.Create();

My project targets .NET Framework 4.6.2.

(Update 1)

For example:

  • C:(...)-9223372036854775808\86dcadfc1e0746649408 - 264 characters - DirectoryNotFoundException
  • C:(...)-9223372036854775808\86dcadfc1e074664 - 260 characters - PathTooLongException
  • C:(...)-9223372036854775808\f872 - 248 characters - no exception

Additional note: I tested with LinqPad (v5.31.00) and there I always get a PathTooLongException with a path 264 characters long.

JoaoRibeiro
  • 808
  • 7
  • 24

1 Answers1

0

Not necessarily.

Invalid path, such as unmapped drives, will thrown DirectoryNotFoundException despite of how long is the path.

Check the source code here,

Gonzalo Lorieto
  • 635
  • 6
  • 24
  • Yes, you are right, but only reducing the size of the path (last folder's name) until I reach 260 characters long, I get a `PathTooLongException`. – JoaoRibeiro Nov 06 '18 at 07:29