Path.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 && path.Length != 0) {
  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. // LAMESPEC: For empty string MS docs say both
  78. // return null AND throw exception. Seems .NET throws.
  79. if (path == String.Empty)
  80. throw new ArgumentException();
  81. if (path == null || GetPathRoot (path) == path)
  82. return null;
  83. CheckArgument.WhitespaceOnly (path);
  84. CheckArgument.PathChars (path);
  85. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  86. if (nLast == 0)
  87. nLast++;
  88. if (nLast > 0)
  89. return path.Substring (0, nLast);
  90. return String.Empty;
  91. }
  92. public static string GetExtension (string path)
  93. {
  94. if (path == null)
  95. return null;
  96. if (path.IndexOfAny (InvalidPathChars) != -1)
  97. throw new ArgumentException ("Illegal characters in path", "path");
  98. int iExt = findExtension (path);
  99. if (iExt > -1)
  100. { // okay it has an extension
  101. return path.Substring (iExt);
  102. }
  103. return string.Empty;
  104. }
  105. public static string GetFileName (string path)
  106. {
  107. if (path == null || path == String.Empty)
  108. return path;
  109. if (path.IndexOfAny (InvalidPathChars) != -1)
  110. throw new ArgumentException ("Illegal characters in path", "path");
  111. int nLast = path.LastIndexOfAny (PathSeparatorChars);
  112. if (nLast >= 0)
  113. return path.Substring (nLast + 1);
  114. return path;
  115. }
  116. public static string GetFileNameWithoutExtension (string path)
  117. {
  118. return ChangeExtension (GetFileName (path), null);
  119. }
  120. public static string GetFullPath (string path)
  121. {
  122. if (path == null)
  123. throw (new ArgumentNullException (
  124. "path",
  125. "You must specify a path when calling System.IO.Path.GetFullPath"));
  126. if (path == String.Empty)
  127. throw new ArgumentException ("The path is not of a legal form", "path");
  128. if (IsPathRooted (path))
  129. return path;
  130. return Directory.GetCurrentDirectory () + DirectorySeparatorStr + path;
  131. }
  132. public static string GetPathRoot (string path)
  133. {
  134. if (path == null)
  135. return null;
  136. if (!IsPathRooted (path))
  137. return String.Empty;
  138. int i = path.IndexOfAny (new char [] {DirectorySeparatorChar, AltDirectorySeparatorChar});
  139. if (i == -1)
  140. return null; // This should never happen, cause IsPathRooted returned true
  141. return path.Substring (0, i + 1);
  142. }
  143. public static string GetTempFileName ()
  144. {
  145. FileStream f = null;
  146. string path;
  147. Random rnd;
  148. int num = 0;
  149. rnd = new Random ();
  150. do {
  151. num = rnd.Next ();
  152. num++;
  153. path = GetTempPath() + DirectorySeparatorChar + "tmp" + num.ToString("x");
  154. try {
  155. f = new FileStream (path, FileMode.CreateNew);
  156. } catch {
  157. }
  158. } while (f == null);
  159. f.Close();
  160. return path;
  161. }
  162. /// <summary>
  163. /// Returns the path of the current systems temp directory
  164. /// </summary>
  165. public static string GetTempPath ()
  166. {
  167. return get_temp_path ();
  168. }
  169. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  170. private static extern string get_temp_path ();
  171. public static bool HasExtension (string path)
  172. {
  173. CheckArgument.Null (path);
  174. CheckArgument.Empty (path);
  175. CheckArgument.WhitespaceOnly (path);
  176. return findExtension (path) > -1;
  177. }
  178. public static bool IsPathRooted (string path)
  179. {
  180. if (path == null || path.Length == 0)
  181. return false;
  182. if (path.IndexOfAny (InvalidPathChars) != -1)
  183. throw new ArgumentException ("Illegal characters in path", "path");
  184. char c = path [0];
  185. return (c == DirectorySeparatorChar ||
  186. c == AltDirectorySeparatorChar ||
  187. (!dirEqualsVolume && path.Length > 1 && path [1] == VolumeSeparatorChar));
  188. }
  189. // private class methods
  190. private static int findExtension (string path)
  191. {
  192. // method should return the index of the path extension
  193. // start or -1 if no valid extension
  194. if (path != null){
  195. int iLastDot = path.LastIndexOf (".");
  196. int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
  197. if (iLastDot > iLastSep)
  198. return iLastDot;
  199. }
  200. return -1;
  201. }
  202. static Path () {
  203. VolumeSeparatorChar = MonoIO.VolumeSeparatorChar;
  204. DirectorySeparatorChar = MonoIO.DirectorySeparatorChar;
  205. AltDirectorySeparatorChar = MonoIO.AltDirectorySeparatorChar;
  206. PathSeparator = MonoIO.PathSeparator;
  207. InvalidPathChars = MonoIO.InvalidPathChars;
  208. // internal fields
  209. DirectorySeparatorStr = DirectorySeparatorChar.ToString ();
  210. PathSeparatorChars = new char [] {
  211. DirectorySeparatorChar,
  212. AltDirectorySeparatorChar,
  213. VolumeSeparatorChar
  214. };
  215. dirEqualsVolume = (DirectorySeparatorChar == VolumeSeparatorChar);
  216. }
  217. }
  218. }