public static String CheckAddLongPathPrefix([NotNull] this String path) { path = path.ThrowIfBlank(); if (path.StartsWith(LongPathPrefix)) { return(path); } var maxPathLimit = NativeMethods.MAX_PATH; if (Uri.TryCreate(path.ThrowIfBlank(), UriKind.Absolute, out var uri) && uri.IsUnc) { // What's going on here? Empirical evidence shows that Windows has trouble dealing with UNC paths // longer than MAX_PATH *minus* the length of the "\\hostname\" prefix. See the following tests: // - UncDirectoryTests.TestDirectoryCreateNearMaxPathLimit // - UncDirectoryTests.TestDirectoryEnumerateDirectoriesNearMaxPathLimit var rootPathLength = 3 + uri.Host.Length; maxPathLimit -= rootPathLength; } if (path.Length < maxPathLimit) { return(path); } return(path.AddLongPathPrefix()); }