12 [__DynamicallyInvokable]
16 [__DynamicallyInvokable]
19 internal const string DirectorySeparatorCharAsString =
"\\";
22 [__DynamicallyInvokable]
26 [__DynamicallyInvokable]
31 [Obsolete(
"Please use GetInvalidPathChars or GetInvalidFileNameChars instead.")]
72 internal static readonly
char[] TrimEndChars = LongPathHelper.s_trimEndChars;
74 private static readonly
char[] RealInvalidPathChars = PathInternal.InvalidPathChars;
76 private static readonly
char[] InvalidPathCharsWithAdditionalChecks =
new char[38]
118 private static readonly
char[] InvalidFileNameChars =
new char[41]
164 [__DynamicallyInvokable]
167 internal static readonly
int MaxPath = 260;
169 private static readonly
int MaxDirectoryLength = PathInternal.MaxComponentLength;
171 internal const int MAX_PATH = 260;
173 internal const int MAX_DIRECTORY_PATH = 248;
175 internal const int MaxLongPath = 32767;
177 private const string LongPathPrefix =
"\\\\?\\";
179 private const string UNCPathPrefix =
"\\\\";
181 private const string UNCLongPathPrefixToInsert =
"?\\UNC\\";
183 private const string UNCLongPathPrefix =
"\\\\?\\UNC\\";
185 private static readonly
char[] s_Base32Char =
new char[32]
227 [__DynamicallyInvokable]
232 CheckInvalidPathChars(path);
234 int num = path.Length;
240 text = path.Substring(0, num);
248 if (extension !=
null && path.Length != 0)
250 if (extension.Length == 0 || extension[0] !=
'.')
267 [__DynamicallyInvokable]
270 return InternalGetDirectoryName(path);
273 [SecuritySafeCritical]
274 private static string InternalGetDirectoryName(
string path)
278 CheckInvalidPathChars(path);
279 string text = NormalizePath(path, fullCheck:
false, AppContextSwitches.UseLegacyPathHandling);
280 if (path.Length > 0 && !CodeAccessSecurityEngine.QuickCheckForAllDemands())
284 string text2 = RemoveLongPathPrefix(path);
286 for (i = 0; i < text2.Length && text2[i] !=
'?' && text2[i] !=
'*'; i++)
298 text = NormalizePath(path, fullCheck:
false, expandShortPaths:
false);
301 catch (PathTooLongException)
304 catch (NotSupportedException)
310 catch (ArgumentException)
315 int rootLength = GetRootLength(path);
316 int length = path.Length;
317 if (length > rootLength)
319 length = path.Length;
320 if (length == rootLength)
327 return path.Substring(0, length);
333 internal static int GetRootLength(
string path)
335 CheckInvalidPathChars(path);
336 if (AppContextSwitches.UseLegacyPathHandling)
338 return LegacyGetRootLength(path);
340 return PathInternal.GetRootLength(path);
343 private static int LegacyGetRootLength(
string path)
346 int length = path.Length;
347 if (length >= 1 && IsDirectorySeparator(path[0]))
350 if (length >= 2 && IsDirectorySeparator(path[1]))
354 for (; i < length; i++)
366 if (length >= 3 && IsDirectorySeparator(path[2]))
374 internal static bool IsDirectorySeparator(
char c)
385 [__DynamicallyInvokable]
388 return (
char[])RealInvalidPathChars.Clone();
393 [__DynamicallyInvokable]
396 return (
char[])InvalidFileNameChars.Clone();
404 [__DynamicallyInvokable]
411 CheckInvalidPathChars(path);
412 int length = path.Length;
419 if (num != length - 1)
421 return path.Substring(num, length - num);
444 [SecuritySafeCritical]
445 [__DynamicallyInvokable]
448 string fullPathInternal = GetFullPathInternal(path);
450 return fullPathInternal;
454 internal static string UnsafeGetFullPath(
string path)
456 string fullPathInternal = GetFullPathInternal(path);
458 return fullPathInternal;
461 internal static string GetFullPathInternal(
string path)
467 return NormalizePath(path, fullCheck:
true);
470 [SecuritySafeCritical]
471 internal static string NormalizePath(
string path,
bool fullCheck)
473 return NormalizePath(path, fullCheck, AppContextSwitches.BlockLongPaths ? 260 : 32767);
476 [SecuritySafeCritical]
477 internal static string NormalizePath(
string path,
bool fullCheck,
bool expandShortPaths)
479 return NormalizePath(path, fullCheck, MaxPath, expandShortPaths);
482 [SecuritySafeCritical]
483 internal static string NormalizePath(
string path,
bool fullCheck,
int maxPathLength)
485 return NormalizePath(path, fullCheck, maxPathLength, expandShortPaths:
true);
488 [SecuritySafeCritical]
489 internal static string NormalizePath(
string path,
bool fullCheck,
int maxPathLength,
bool expandShortPaths)
491 if (AppContextSwitches.UseLegacyPathHandling)
493 return LegacyNormalizePath(path, fullCheck, maxPathLength, expandShortPaths);
495 if (PathInternal.IsExtended(path))
500 text = (fullCheck ? NewNormalizePath(path, maxPathLength, expandShortPaths:
true) : NewNormalizePathLimitedChecks(path, maxPathLength, expandShortPaths));
501 if (
string.IsNullOrWhiteSpace(text))
503 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
508 [SecuritySafeCritical]
509 private static string NewNormalizePathLimitedChecks(
string path,
int maxPathLength,
bool expandShortPaths)
511 string text = PathInternal.NormalizeDirectorySeparators(path);
512 if (PathInternal.IsPathTooLong(text) || PathInternal.AreSegmentsTooLong(text))
514 throw new PathTooLongException();
516 if (expandShortPaths && text.IndexOf(
'~') != -1)
520 return LongPathHelper.GetLongPathName(text);
530 [SecuritySafeCritical]
531 private static string NewNormalizePath(
string path,
int maxPathLength,
bool expandShortPaths)
533 if (path.IndexOf(
'\0') != -1)
535 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidPathChars"));
537 if (
string.IsNullOrWhiteSpace(path))
539 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
541 return LongPathHelper.Normalize(path, (uint)maxPathLength, !PathInternal.IsDevice(path), expandShortPaths);
545 internal unsafe
static string LegacyNormalizePath(
string path,
bool fullCheck,
int maxPathLength,
bool expandShortPaths)
549 path = path.TrimEnd(TrimEndChars);
550 if (PathInternal.AnyPathHasIllegalCharacters(path))
552 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidPathChars"));
556 PathHelper pathHelper;
557 if (path.Length + 1 > MaxPath)
559 pathHelper =
new PathHelper(path.Length + MaxPath, maxPathLength);
563 char* charArrayPtr = stackalloc
char[MaxPath];
564 pathHelper =
new PathHelper(charArrayPtr, MaxPath);
577 pathHelper.Append(
'\\');
581 for (; i < path.Length; i++)
591 if (path[num6] !=
'.')
593 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
597 if (flag2 && num2 > 2)
599 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
601 if (path[num6 + 1] ==
'.')
603 for (
int j = num6 + 2; j < num6 + num2; j++)
607 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
616 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
623 pathHelper.Append(
'.');
625 pathHelper.Append(
'.');
646 pathHelper.TryExpandShortFileName();
649 int num7 = pathHelper.Length - 1;
650 if (num7 - num5 > MaxDirectoryLength)
652 throw new PathTooLongException(Environment.GetResourceString(
"IO.PathTooLong"));
666 if (c ==
'~' && expandShortPaths)
673 char c2 = (i > 0) ? path[i - 1] :
' ';
674 if (num2 != 0 || num3 < 1 || c2 ==
' ')
676 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
682 for (k = 0; k < pathHelper.Length && pathHelper[k] ==
' '; k++)
687 pathHelper.Length = 0;
688 pathHelper.Append(c2);
695 num3 += 1 + num2 + num;
697 if (num2 != 0 || num != 0)
699 int num8 = (num4 >= 0) ? (i - num4 - 1) : i;
702 for (
int l = 0; l < num8; l++)
704 pathHelper.Append(path[num4 + 1 + l]);
710 pathHelper.Append(c);
713 if (pathHelper.Length - 1 - num5 > MaxDirectoryLength)
715 throw new PathTooLongException(Environment.GetResourceString(
"IO.PathTooLong"));
717 if (num3 == 0 && num2 != 0)
720 if (path[num9] !=
'.')
722 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
726 if (flag2 && num2 > 2)
728 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
730 if (path[num9 + 1] ==
'.')
732 for (
int m = num9 + 2; m < num9 + num2; m++)
736 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
745 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
752 pathHelper.Append(
'.');
754 pathHelper.Append(
'.');
756 if (pathHelper.Length == 0)
758 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegal"));
760 if (fullCheck && (pathHelper.OrdinalStartsWith(
"http:", ignoreCase:
false) || pathHelper.OrdinalStartsWith(
"file:", ignoreCase:
false)))
762 throw new ArgumentException(Environment.GetResourceString(
"Argument_PathUriFormatNotSupported"));
766 pathHelper.TryExpandShortFileName();
771 num10 = pathHelper.GetFullPathName();
773 for (
int n = 0; n < pathHelper.Length; n++)
779 if (pathHelper[n] ==
'~' && expandShortPaths)
784 if (flag4 && !pathHelper.TryExpandShortFileName())
787 for (
int num12 = pathHelper.Length - 1; num12 >= 0; num12--)
797 if (pathHelper.Length >= maxPathLength)
799 throw new PathTooLongException(Environment.GetResourceString(
"IO.PathTooLong"));
801 int lenSavedName = pathHelper.Length - num11 - 1;
802 pathHelper.Fixup(lenSavedName, num11);
806 if (num10 != 0 && pathHelper.Length > 1 && pathHelper[0] ==
'\\' && pathHelper[1] ==
'\\')
809 for (num13 = 2; num13 < num10; num13++)
811 if (pathHelper[num13] ==
'\\')
819 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathIllegalUNC"));
821 if (pathHelper.OrdinalStartsWith(
"\\\\?\\globalroot", ignoreCase:
true))
823 throw new ArgumentException(Environment.GetResourceString(
"Arg_PathGlobalRoot"));
826 if (pathHelper.Length >= maxPathLength)
828 throw new PathTooLongException(Environment.GetResourceString(
"IO.PathTooLong"));
837 __Error.WinIOError(num14, path);
840 string text = pathHelper.ToString();
848 internal static bool HasLongPathPrefix(
string path)
850 if (AppContextSwitches.UseLegacyPathHandling)
854 return PathInternal.IsExtended(path);
857 internal static string AddLongPathPrefix(
string path)
859 if (AppContextSwitches.UseLegacyPathHandling)
867 return path.Insert(2,
"?\\UNC\\");
869 return "\\\\?\\" + path;
871 return PathInternal.EnsureExtendedPrefix(path);
874 internal static string RemoveLongPathPrefix(
string path)
876 if (AppContextSwitches.UseLegacyPathHandling)
884 return path.Remove(2, 6);
886 return path.Substring(4);
888 return PathInternal.RemoveExtendedPrefix(path);
893 if (AppContextSwitches.UseLegacyPathHandling)
895 if (!PathInternal.StartsWithOrdinal(pathSB,
"\\\\?\\"))
899 if (PathInternal.StartsWithOrdinal(pathSB,
"\\\\?\\UNC\\", ignoreCase:
true))
901 return pathSB.
Remove(2, 6);
903 return pathSB.
Remove(0, 4);
905 return PathInternal.RemoveExtendedPrefix(pathSB);
913 [__DynamicallyInvokable]
918 CheckInvalidPathChars(path);
919 int length = path.Length;
926 return path.Substring(num + 1, length - num - 1);
938 [__DynamicallyInvokable]
945 if ((length = path.LastIndexOf(
'.')) == -1)
949 return path.Substring(0, length);
960 [__DynamicallyInvokable]
967 path = NormalizePath(path, fullCheck:
false, expandShortPaths:
false);
968 return path.Substring(0, GetRootLength(path));
974 [SecuritySafeCritical]
975 [__DynamicallyInvokable]
980 uint tempPath = Win32Native.GetTempPath(260, stringBuilder);
981 string path = stringBuilder.
ToString();
984 __Error.WinIOError();
986 return GetFullPathInternal(path);
989 internal static bool IsRelative(
string path)
991 return PathInternal.IsPartiallyQualified(path);
996 [__DynamicallyInvokable]
999 byte[] array =
new byte[10];
1004 rNGCryptoServiceProvider.
GetBytes(array);
1005 char[] array2 = ToBase32StringSuitableForDirName(array).ToCharArray();
1007 return new string(array2, 0, 12);
1011 rNGCryptoServiceProvider?.
Dispose();
1018 [SecuritySafeCritical]
1019 [__DynamicallyInvokable]
1022 return InternalGetTempFileName(checkHost:
true);
1026 internal static string UnsafeGetTempFileName()
1028 return InternalGetTempFileName(checkHost:
false);
1032 private static string InternalGetTempFileName(
bool checkHost)
1037 if (Win32Native.GetTempFileName(tempPath,
"tmp", 0u, stringBuilder) == 0)
1039 __Error.WinIOError();
1050 [__DynamicallyInvokable]
1055 CheckInvalidPathChars(path);
1056 int num = path.Length;
1062 if (num != path.Length - 1)
1083 [__DynamicallyInvokable]
1088 CheckInvalidPathChars(path);
1089 int length = path.Length;
1106 [__DynamicallyInvokable]
1107 public static string Combine(
string path1,
string path2)
1109 if (path1 ==
null || path2 ==
null)
1113 CheckInvalidPathChars(path1);
1114 CheckInvalidPathChars(path2);
1115 return CombineNoChecks(path1, path2);
1127 [__DynamicallyInvokable]
1128 public static string Combine(
string path1,
string path2,
string path3)
1130 if (path1 ==
null || path2 ==
null || path3 ==
null)
1134 CheckInvalidPathChars(path1);
1135 CheckInvalidPathChars(path2);
1136 CheckInvalidPathChars(path3);
1137 return CombineNoChecks(CombineNoChecks(path1, path2), path3);
1150 public static string Combine(
string path1,
string path2,
string path3,
string path4)
1152 if (path1 ==
null || path2 ==
null || path3 ==
null || path4 ==
null)
1154 throw new ArgumentNullException((path1 ==
null) ?
"path1" : ((path2 ==
null) ?
"path2" : ((path3 ==
null) ?
"path3" :
"path4")));
1156 CheckInvalidPathChars(path1);
1157 CheckInvalidPathChars(path2);
1158 CheckInvalidPathChars(path3);
1159 CheckInvalidPathChars(path4);
1160 return CombineNoChecks(CombineNoChecks(CombineNoChecks(path1, path2), path3), path4);
1168 [__DynamicallyInvokable]
1169 public static string Combine(params
string[] paths)
1177 for (
int i = 0; i < paths.Length; i++)
1179 if (paths[i] ==
null)
1183 if (paths[i].Length != 0)
1185 CheckInvalidPathChars(paths[i]);
1189 num = paths[i].Length;
1193 num += paths[i].Length;
1195 char c = paths[i][paths[i].Length - 1];
1202 StringBuilder stringBuilder = StringBuilderCache.Acquire(num);
1203 for (
int j = num2; j < paths.Length; j++)
1205 if (paths[j].Length == 0)
1209 if (stringBuilder.
Length == 0)
1211 stringBuilder.
Append(paths[j]);
1214 char c2 = stringBuilder[stringBuilder.
Length - 1];
1219 stringBuilder.
Append(paths[j]);
1221 return StringBuilderCache.GetStringAndRelease(stringBuilder);
1224 private static string CombineNoChecks(
string path1,
string path2)
1226 if (path2.Length == 0)
1230 if (path1.Length == 0)
1238 char c = path1[path1.Length - 1];
1241 return path1 +
"\\" + path2;
1243 return path1 + path2;
1246 internal static string ToBase32StringSuitableForDirName(
byte[] buff)
1253 byte b = (byte)((num2 < num) ? buff[num2++] : 0);
1254 byte b2 = (byte)((num2 < num) ? buff[num2++] : 0);
1255 byte b3 = (byte)((num2 < num) ? buff[num2++] : 0);
1256 byte b4 = (byte)((num2 < num) ? buff[num2++] : 0);
1257 byte b5 = (byte)((num2 < num) ? buff[num2++] : 0);
1258 stringBuilder.
Append(s_Base32Char[b & 0x1F]);
1259 stringBuilder.
Append(s_Base32Char[b2 & 0x1F]);
1260 stringBuilder.
Append(s_Base32Char[b3 & 0x1F]);
1261 stringBuilder.
Append(s_Base32Char[b4 & 0x1F]);
1262 stringBuilder.
Append(s_Base32Char[b5 & 0x1F]);
1263 stringBuilder.
Append(s_Base32Char[((b & 0xE0) >> 5) | ((b4 & 0x60) >> 2)]);
1264 stringBuilder.
Append(s_Base32Char[((b2 & 0xE0) >> 5) | ((b5 & 0x60) >> 2)]);
1265 b3 = (byte)(b3 >> 5);
1266 if ((b4 & 0x80) != 0)
1268 b3 = (byte)(b3 | 8);
1270 if ((b5 & 0x80) != 0)
1272 b3 = (byte)(b3 | 0x10);
1274 stringBuilder.
Append(s_Base32Char[b3]);
1277 return StringBuilderCache.GetStringAndRelease(stringBuilder);
1280 internal static void CheckSearchPattern(
string searchPattern)
1287 if (num + 2 == searchPattern.Length)
1289 throw new ArgumentException(Environment.GetResourceString(
"Arg_InvalidSearchPattern"));
1295 searchPattern = searchPattern.Substring(num + 2);
1300 throw new ArgumentException(Environment.GetResourceString(
"Arg_InvalidSearchPattern"));
1303 internal static void CheckInvalidPathChars(
string path,
bool checkAdditional =
false)
1307 throw new ArgumentNullException(
"path");
1309 if (PathInternal.HasIllegalCharacters(path, checkAdditional))
1311 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidPathChars"));
1315 internal static string InternalCombine(
string path1,
string path2)
1317 if (path1 ==
null || path2 ==
null)
1319 throw new ArgumentNullException((path1 ==
null) ?
"path1" :
"path2");
1321 CheckInvalidPathChars(path1);
1322 CheckInvalidPathChars(path2);
1323 if (path2.Length == 0)
1325 throw new ArgumentException(Environment.GetResourceString(
"Argument_PathEmpty"),
"path2");
1329 throw new ArgumentException(Environment.GetResourceString(
"Arg_Path2IsRooted"),
"path2");
1331 int length = path1.Length;
1336 char c = path1[length - 1];
1339 return path1 +
"\\" + path2;
1341 return path1 + path2;
static string GetExtension(string path)
Returns the extension of the specified path string.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
FileIOPermissionAccess
Specifies the type of file access requested.
static char [] GetInvalidPathChars()
Gets an array containing the characters that are not allowed in path names.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
static string Combine(string path1, string path2)
Combines two strings into a path.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
static readonly char [] InvalidPathChars
Provides a platform-specific array of characters that cannot be specified in path string arguments pa...
static string GetPathRoot(string path)
Gets the root directory information of the specified path.
static readonly char AltDirectorySeparatorChar
Provides a platform-specific alternate character used to separate directory levels in a path string t...
override void GetBytes(byte[] data)
Fills an array of bytes with a cryptographically strong sequence of random values.
static string GetFileName(string path)
Returns the file name and extension of the specified path string.
static readonly char DirectorySeparatorChar
Provides a platform-specific character used to separate directory levels in a path string that reflec...
static string GetTempFileName()
Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.
Implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cry...
override void Dispose(bool disposing)
When overridden in a derived class, releases the unmanaged resources used by the T:System....
static string Combine(string path1, string path2, string path3)
Combines three strings into a path.
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
static string GetFullPath(string path)
Returns the absolute path for the specified path string.
static readonly char PathSeparator
A platform-specific separator character used to separate path strings in environment variables.
static char [] GetInvalidFileNameChars()
Gets an array containing the characters that are not allowed in file names.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
static bool HasExtension(string path)
Determines whether a path includes a file name extension.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
static string GetTempPath()
Returns the path of the current user's temporary folder.
Controls access to system and user environment variables. This class cannot be inherited.
static string GetDirectoryName(string path)
Returns the directory information for the specified path string.
Controls the ability to access files and folders. This class cannot be inherited.
static string GetRandomFileName()
Returns a random folder name or file name.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
static string Combine(string path1, string path2, string path3, string path4)
Combines four strings into a path.
static bool IsPathRooted(string path)
Gets a value indicating whether the specified path string contains a root.
The exception that is thrown when a security error is detected.
Performs operations on T:System.String instances that contain file or directory path information....
static string Combine(params string[] paths)
Combines an array of strings into a path.
static string GetFileNameWithoutExtension(string path)
Returns the file name of the specified path string without the extension.
static readonly char VolumeSeparatorChar
Provides a platform-specific volume separator character.
StringBuilder Remove(int startIndex, int length)
Removes the specified range of characters from this instance.
static string ChangeExtension(string path, string extension)
Changes the extension of a path string.