Path.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. //
  8. // Author: Jim Richardson, [email protected]
  9. // Dan Lewis ([email protected])
  10. // Gonzalo Paniagua Javier ([email protected])
  11. // Created: Saturday, August 11, 2001
  12. //
  13. //------------------------------------------------------------------------------
  14. using System;
  15. using System.Runtime.CompilerServices;
  16. namespace System.IO
  17. {
  18. public sealed class Path
  19. {
  20. public static readonly char AltDirectorySeparatorChar;
  21. public static readonly char DirectorySeparatorChar;
  22. public static readonly char[] InvalidPathChars;
  23. public static readonly char PathSeparator;
  24. internal static readonly string DirectorySeparatorStr;
  25. public static readonly char VolumeSeparatorChar;
  26. private static readonly char[] PathSeparatorChars;
  27. private static bool dirEqualsVolume;
  28. private Path () {}
  29. // class methods
  30. public static string ChangeExtension (string path, string extension)
  31. {
  32. if (path == null)
  33. {
  34. return null;
  35. }
  36. if (path.IndexOfAny (InvalidPathChars) != -1)
  37. throw new ArgumentException ("Illegal characters in path", "path");
  38. int iExt = findExtension (path);
  39. if (extension != null) {
  40. if (extension [0] != '.')
  41. extension = "." + extension;
  42. } else
  43. extension = String.Empty;
  44. if (iExt < 0) {
  45. return path + extension;
  46. } else if (iExt > 0) {
  47. string temp = path.Substring (0, iExt);
  48. return temp + extension;
  49. }
  50. return extension;
  51. }
  52. public static string Combine (string path1, string path2)
  53. {
  54. if (path1 == null)
  55. throw new ArgumentNullException ("path1");
  56. if (path2 == null)
  57. throw new ArgumentNullException ("path2");
  58. if (path1 == String.Empty)
  59. return path2;
  60. if (path2 == String.Empty)
  61. return path1;
  62. if (path1.IndexOfAny (InvalidPathChars) != -1)
  63. throw new ArgumentException ("Illegal characters in path", "path1");
  64. if (path2.IndexOfAny (InvalidPathChars) != -1)
  65. throw new ArgumentException ("Illegal characters in path", "path2");
  66. //TODO???: UNC names
  67. // LAMESPEC: MS says that if path1 is not empty and path2 is a full path
  68. // it should throw ArgumentException
  69. if (IsPathRooted (path2))
  70. return path2;
  71. if (Array.IndexOf (PathSeparatorChars, path1 [path1.Length - 1]) == -1)
  72. return path1 + DirectorySeparatorChar + path2;
  73. return path1 + path2;
  74. }
  75. public static string GetDirectoryName (string path)
  76. {
  77. if (path != null)
  78. {
  79. CheckArgument.Empty (path);
  80. CheckArgument.WhitespaceOnly (path);
  81. CheckArgument.PathChars (path);
  82. if (path.Length > 0)
  83. {
  84. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  85. if (nLast > 0)
  86. return path.Substring (0, nLast);
  87. else
  88. return String.Empty;
  89. }
  90. }
  91. return path;
  92. }
  93. public static string GetExtension (string path)
  94. {
  95. if (path == null)
  96. return null;
  97. if (path.IndexOfAny (InvalidPathChars) != -1)
  98. throw new ArgumentException ("Illegal characters in path", "path");
  99. int iExt = findExtension (path);
  100. if (iExt > -1)
  101. { // okay it has an extension
  102. return path.Substring (iExt);
  103. }
  104. return string.Empty;
  105. }
  106. public static string GetFileName (string path)
  107. {
  108. if (path == null || path == String.Empty)
  109. return path;
  110. if (path.IndexOfAny (InvalidPathChars) != -1)
  111. throw new ArgumentException ("Illegal characters in path", "path");
  112. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  113. if (nLast > 0)
  114. return path.Substring (nLast + 1);
  115. return nLast == 0 ? null : path;
  116. }
  117. public static string GetFileNameWithoutExtension (string path)
  118. {
  119. return ChangeExtension (GetFileName (path), null);
  120. }
  121. public static string GetFullPath (string path)
  122. {
  123. if (path == null)
  124. throw (new ArgumentNullException (
  125. "path",
  126. "You must specify a path when calling System.IO.Path.GetFullPath"));
  127. if (path == String.Empty)
  128. throw new ArgumentException ("The path is not of a legal form", "path");
  129. if (IsPathRooted (path))
  130. return path;
  131. return Directory.GetCurrentDirectory () + DirectorySeparatorStr + path;
  132. }
  133. public static string GetPathRoot (string path)
  134. {
  135. if (path == null)
  136. return null;
  137. if (!IsPathRooted (path))
  138. return String.Empty;
  139. int i = path.IndexOfAny (new char [] {DirectorySeparatorChar, AltDirectorySeparatorChar});
  140. if (i == -1)
  141. return null; // This should never happen, cause IsPathRooted returned true
  142. return path.Substring (0, i + 1);
  143. }
  144. public static string GetTempFileName ()
  145. {
  146. FileStream f = null;
  147. string path;
  148. Random rnd;
  149. int num = 0;
  150. rnd = new Random ();
  151. do {
  152. num = rnd.Next ();
  153. num++;
  154. path = GetTempPath() + DirectorySeparatorChar + "tmp" + num.ToString("x");
  155. try {
  156. f = new FileStream (path, FileMode.CreateNew);
  157. } catch {
  158. }
  159. } while (f == null);
  160. f.Close();
  161. return path;
  162. }
  163. /// <summary>
  164. /// Returns the path of the current systems temp directory
  165. /// </summary>
  166. public static string GetTempPath ()
  167. {
  168. return get_temp_path ();
  169. }
  170. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  171. private static extern string get_temp_path ();
  172. public static bool HasExtension (string path)
  173. {
  174. CheckArgument.Null (path);
  175. CheckArgument.Empty (path);
  176. CheckArgument.WhitespaceOnly (path);
  177. return findExtension (path) > -1;
  178. }
  179. public static bool IsPathRooted (string path)
  180. {
  181. if (path == null || path.Length == 0)
  182. return false;
  183. if (path.IndexOfAny (InvalidPathChars) != -1)
  184. throw new ArgumentException ("Illegal characters in path", "path");
  185. char c = path [0];
  186. return (c == DirectorySeparatorChar ||
  187. c == AltDirectorySeparatorChar ||
  188. (!dirEqualsVolume && path.Length > 1 && path [1] == VolumeSeparatorChar));
  189. }
  190. // private class methods
  191. private static int findExtension (string path)
  192. {
  193. // method should return the index of the path extension
  194. // start or -1 if no valid extension
  195. if (path != null){
  196. int iLastDot = path.LastIndexOf (".");
  197. int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
  198. if (iLastDot > iLastSep)
  199. return iLastDot;
  200. }
  201. return -1;
  202. }
  203. static Path () {
  204. VolumeSeparatorChar = MonoIO.VolumeSeparatorChar;
  205. DirectorySeparatorChar = MonoIO.DirectorySeparatorChar;
  206. AltDirectorySeparatorChar = MonoIO.AltDirectorySeparatorChar;
  207. PathSeparator = MonoIO.PathSeparator;
  208. InvalidPathChars = MonoIO.InvalidPathChars;
  209. // internal fields
  210. DirectorySeparatorStr = DirectorySeparatorChar.ToString ();
  211. PathSeparatorChars = new char [] {
  212. DirectorySeparatorChar,
  213. AltDirectorySeparatorChar,
  214. VolumeSeparatorChar
  215. };
  216. dirEqualsVolume = (DirectorySeparatorChar == VolumeSeparatorChar);
  217. }
  218. }
  219. }