Path.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //------------------------------------------------------------------------------
  2. //
  3. // System.IO.Path.cs
  4. //
  5. // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
  6. // Copyright (C) 2002 Ximian, Inc. (http://www.ximian.com)
  7. // Copyright (C) 2003 Ben Maurer
  8. //
  9. // Author: Jim Richardson, [email protected]
  10. // Dan Lewis ([email protected])
  11. // Gonzalo Paniagua Javier ([email protected])
  12. // Ben Maurer ([email protected])
  13. // Created: Saturday, August 11, 2001
  14. //
  15. //------------------------------------------------------------------------------
  16. using System;
  17. using System.Runtime.CompilerServices;
  18. namespace System.IO
  19. {
  20. public sealed class Path
  21. {
  22. public static readonly char AltDirectorySeparatorChar;
  23. public static readonly char DirectorySeparatorChar;
  24. public static readonly char[] InvalidPathChars;
  25. public static readonly char PathSeparator;
  26. internal static readonly string DirectorySeparatorStr;
  27. public static readonly char VolumeSeparatorChar;
  28. private static readonly char[] PathSeparatorChars;
  29. private static bool dirEqualsVolume;
  30. private Path () {}
  31. // class methods
  32. public static string ChangeExtension (string path, string extension)
  33. {
  34. if (path == null)
  35. {
  36. return null;
  37. }
  38. if (path.IndexOfAny (InvalidPathChars) != -1)
  39. throw new ArgumentException ("Illegal characters in path", "path");
  40. int iExt = findExtension (path);
  41. if (extension != null && path.Length != 0) {
  42. if (extension [0] != '.')
  43. extension = "." + extension;
  44. } else
  45. extension = String.Empty;
  46. if (iExt < 0) {
  47. return path + extension;
  48. } else if (iExt > 0) {
  49. string temp = path.Substring (0, iExt);
  50. return temp + extension;
  51. }
  52. return extension;
  53. }
  54. public static string Combine (string path1, string path2)
  55. {
  56. if (path1 == null)
  57. throw new ArgumentNullException ("path1");
  58. if (path2 == null)
  59. throw new ArgumentNullException ("path2");
  60. if (path1 == String.Empty)
  61. return path2;
  62. if (path2 == String.Empty)
  63. return path1;
  64. if (path1.IndexOfAny (InvalidPathChars) != -1)
  65. throw new ArgumentException ("Illegal characters in path", "path1");
  66. if (path2.IndexOfAny (InvalidPathChars) != -1)
  67. throw new ArgumentException ("Illegal characters in path", "path2");
  68. //TODO???: UNC names
  69. // LAMESPEC: MS says that if path1 is not empty and path2 is a full path
  70. // it should throw ArgumentException
  71. if (IsPathRooted (path2))
  72. return path2;
  73. if (Array.IndexOf (PathSeparatorChars, path1 [path1.Length - 1]) == -1)
  74. return path1 + DirectorySeparatorChar + path2;
  75. return path1 + path2;
  76. }
  77. public static string GetDirectoryName (string path)
  78. {
  79. // LAMESPEC: For empty string MS docs say both
  80. // return null AND throw exception. Seems .NET throws.
  81. if (path == String.Empty)
  82. throw new ArgumentException();
  83. if (path == null || GetPathRoot (path) == path)
  84. return null;
  85. CheckArgument.WhitespaceOnly (path);
  86. CheckArgument.PathChars (path);
  87. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  88. if (nLast == 0)
  89. nLast++;
  90. if (nLast > 0)
  91. return path.Substring (0, nLast);
  92. return String.Empty;
  93. }
  94. public static string GetExtension (string path)
  95. {
  96. if (path == null)
  97. return null;
  98. if (path.IndexOfAny (InvalidPathChars) != -1)
  99. throw new ArgumentException ("Illegal characters in path", "path");
  100. int iExt = findExtension (path);
  101. if (iExt > -1)
  102. { // okay it has an extension
  103. return path.Substring (iExt);
  104. }
  105. return string.Empty;
  106. }
  107. public static string GetFileName (string path)
  108. {
  109. if (path == null || path == String.Empty)
  110. return path;
  111. if (path.IndexOfAny (InvalidPathChars) != -1)
  112. throw new ArgumentException ("Illegal characters in path", "path");
  113. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  114. if (nLast >= 0)
  115. return path.Substring (nLast + 1);
  116. return path;
  117. }
  118. public static string GetFileNameWithoutExtension (string path)
  119. {
  120. return ChangeExtension (GetFileName (path), null);
  121. }
  122. public static string GetFullPath (string path)
  123. {
  124. if (path == null)
  125. throw (new ArgumentNullException (
  126. "path",
  127. "You must specify a path when calling System.IO.Path.GetFullPath"));
  128. if (path.Trim () == String.Empty)
  129. throw new ArgumentException ("The path is not of a legal form", "path");
  130. if (!IsPathRooted (path))
  131. path = Directory.GetCurrentDirectory () + DirectorySeparatorStr + path;
  132. return CanonicalizePath (path);
  133. }
  134. static bool IsDsc (char c) {
  135. return c == DirectorySeparatorChar || c == AltDirectorySeparatorChar;
  136. }
  137. public static string GetPathRoot (string path)
  138. {
  139. if (path == null) return null;
  140. if (!IsPathRooted (path)) return String.Empty;
  141. if (DirectorySeparatorChar == '/') {
  142. // UNIX
  143. return IsDsc (path [0]) ? DirectorySeparatorChar.ToString () : String.Empty;
  144. } else {
  145. // Windows
  146. int len = 2;
  147. if (path.Length <= 2) return String.Empty;
  148. if (IsDsc (path [0]) && IsDsc (path[1])) {
  149. // UNC: \\server or \\server\share
  150. // Get server
  151. while (len < path.Length && !IsDsc (path [len])) len++;
  152. // Get share
  153. while (len < path.Length && !IsDsc (path [len])) len++;
  154. } else if (path[1] == VolumeSeparatorChar) {
  155. // C:\folder
  156. if (path.Length >= 3 && (IsDsc (path [2]))) len++;
  157. }
  158. return path.Substring (0, len);
  159. }
  160. }
  161. public static string GetTempFileName ()
  162. {
  163. FileStream f = null;
  164. string path;
  165. Random rnd;
  166. int num = 0;
  167. rnd = new Random ();
  168. do {
  169. num = rnd.Next ();
  170. num++;
  171. path = GetTempPath() + DirectorySeparatorChar + "tmp" + num.ToString("x");
  172. try {
  173. f = new FileStream (path, FileMode.CreateNew);
  174. } catch {
  175. }
  176. } while (f == null);
  177. f.Close();
  178. return path;
  179. }
  180. /// <summary>
  181. /// Returns the path of the current systems temp directory
  182. /// </summary>
  183. public static string GetTempPath ()
  184. {
  185. return get_temp_path ();
  186. }
  187. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  188. private static extern string get_temp_path ();
  189. public static bool HasExtension (string path)
  190. {
  191. CheckArgument.Null (path);
  192. CheckArgument.Empty (path);
  193. CheckArgument.WhitespaceOnly (path);
  194. return findExtension (path) > -1;
  195. }
  196. public static bool IsPathRooted (string path)
  197. {
  198. if (path == null || path.Length == 0)
  199. return false;
  200. if (path.IndexOfAny (InvalidPathChars) != -1)
  201. throw new ArgumentException ("Illegal characters in path", "path");
  202. char c = path [0];
  203. return (c == DirectorySeparatorChar ||
  204. c == AltDirectorySeparatorChar ||
  205. (!dirEqualsVolume && path.Length > 1 && path [1] == VolumeSeparatorChar));
  206. }
  207. // private class methods
  208. private static int findExtension (string path)
  209. {
  210. // method should return the index of the path extension
  211. // start or -1 if no valid extension
  212. if (path != null){
  213. int iLastDot = path.LastIndexOf (".");
  214. int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
  215. if (iLastDot > iLastSep)
  216. return iLastDot;
  217. }
  218. return -1;
  219. }
  220. static Path () {
  221. VolumeSeparatorChar = MonoIO.VolumeSeparatorChar;
  222. DirectorySeparatorChar = MonoIO.DirectorySeparatorChar;
  223. AltDirectorySeparatorChar = MonoIO.AltDirectorySeparatorChar;
  224. PathSeparator = MonoIO.PathSeparator;
  225. InvalidPathChars = MonoIO.InvalidPathChars;
  226. // internal fields
  227. DirectorySeparatorStr = DirectorySeparatorChar.ToString ();
  228. PathSeparatorChars = new char [] {
  229. DirectorySeparatorChar,
  230. AltDirectorySeparatorChar,
  231. VolumeSeparatorChar
  232. };
  233. dirEqualsVolume = (DirectorySeparatorChar == VolumeSeparatorChar);
  234. }
  235. static string CanonicalizePath (string path) {
  236. // STEP 1: Check for empty string
  237. if (path == null) return path;
  238. path = path.Trim ();
  239. if (path == String.Empty) return path;
  240. // STEP 2: Check to see if this is only a root
  241. string root = GetPathRoot (path);
  242. if (root == path) return path;
  243. string dir = GetDirectoryName (path).Substring (path.IndexOfAny (new char [] {DirectorySeparatorChar, AltDirectorySeparatorChar}) + 1);
  244. if (dir == String.Empty) return path;
  245. // STEP 3: split the directories, this gets rid of consecutative "/"'s
  246. string [] dirs = path.Split (DirectorySeparatorChar, AltDirectorySeparatorChar);
  247. // STEP 4: Get rid of directories containing . and ..
  248. int target = 0;
  249. for (int i = 0; i < dirs.Length; i++) {
  250. if (dirs [i] == "." || dirs [i] == String.Empty) continue;
  251. else if (dirs [i] == "..") {
  252. if (target != 0) target--;
  253. }
  254. else
  255. dirs [target++] = dirs [i];
  256. }
  257. // STEP 5: Combine everything.
  258. if (target == 0)
  259. return root;
  260. else
  261. return root + String.Join (DirectorySeparatorStr, dirs, 0, target);
  262. }
  263. }
  264. }