-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strange behavior of Directory.CreateDirectory #23807
Comments
@JeremyKuhne can doubtless explain ... |
You have to know quite a bit about how Windows handles paths to fully understand what is happening. I blogged about path handling in great detail starting with https://blogs.msdn.microsoft.com/jeremykuhne/2016/04/21/path-format-overview/. The bottom line is that there are no "real" path limits in most file systems (NTFS, FAT, ext4, etc.). Limits in Windows APIs are from a few sources:
The lower level Windows APIs (the NT apis) all use While you might think adding the
Bottom line is that there is no way to tell exactly what the limit will be. Eventually somewhere deep in Windows the code will either see that a path it wants to create won't fit and return the path too long error or it will end up truncating a segment of the path and find that When .NET creates directories we can't know ahead of time that a given path will be too long, even if it is longer than 32767 characters to start with, due to normalization and parsing that happens in Windows. Rather than block a fixed path length and block some legitimate paths we just always make the attempt and let Windows define/report the error. We just go through and create every subdirectory that doesn't exist. Trying to clean up would be problematic given the async nature of IO and how corner these cases are. Fyi: As a general rule we don't try to second guess the errors the OS will give. Being over helpful blocks real customer scenarios as we won't always get it right (and will likely not get it right as the OS is always changing). |
This seems answered. |
I found strange behaviors in
Directory.CreateDirectory
in my .net core2.0 app. My test code looks like:I change the value of
lastLength
a few times, here is the question:Q1:
lastLength = 224, path.Length = 32739
everything is working fine. What's the magic of 32739?Q2:
225 <= lastLength <= 247, 32740 <= path.Length <= 32762
throwsSystem.IO.PathTooLongException
, meanwhileDirectory.Exists
printsFalse
. However I can see the first few level directories are created(not all directories), which means the creation is partially done with an exception. Why?Q3:
248 <= lastLength <= 251, 32763 <= path.Length <= 32766
cases are almost the same as those in Q2, except the exception isSystem.IO.DirectoryNotFoundException
, why?lastLength > = 252, path.Length >= 32767
fails withSystem.IO.PathTooLongException
, no directory is created, which is as expected.[EDIT] Add C# syntax highlight by @karelz
The text was updated successfully, but these errors were encountered: