1 //------------------------------------------------------------------------------
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
7 // Author: Jim Richardson, develop@wtfo-guru.com
8 // Created: Saturday, August 11, 2001
10 //------------------------------------------------------------------------------
19 private static OpSys _os = Platform.OS;
21 public static readonly char AltDirectorySeparatorChar = _os.AltDirectorySeparator;
22 public static readonly char DirectorySeparatorChar = _os.DirectorySeparator;
23 public static readonly char[] InvalidPathChars = _os.InvalidPathChars;
24 public static readonly char PathSeparator = _os.PathSeparator;
25 public static readonly char VolumeSeparatorChar = _os.VolumeSeparator;
27 private static readonly char[] PathSeparatorChars = { DirectorySeparatorChar,
28 AltDirectorySeparatorChar,
29 VolumeSeparatorChar };
32 public static string ChangeExtension (string path, string extension)
39 int iExt = findExtension (path);
43 return extension == null ? path : path + extension;
47 string temp = path.Substring (0, iExt);
48 if (extension != null)
50 return temp + extension;
59 public static string Combine (string path1, string path2)
61 if (path1 == null || path2 == null)
66 CheckArgument.Empty (path2);
68 // TODO: Check for invalid DirectoryInfo characters
69 // although I don't think it is necesary for linux
71 // TODO: Verify functionality further after NUnit tests written
72 // since the documentation was rather sketchy
74 if (IsPathRooted (path2))
76 if (path1.Equals (string.Empty))
80 throw new ArgumentException ("Rooted path");
83 string dirSep = new string (DirectorySeparatorChar, 1);
84 string altSep = new string (AltDirectorySeparatorChar, 1);
86 bool b1 = path1.EndsWith (dirSep) || path1.EndsWith (dirSep);
87 bool b2 = path2.StartsWith (dirSep) || path2.StartsWith (altSep);
90 throw new ArgumentException ("Invalid combination");
95 return path1 + dirSep + path2;
101 public static string GetDirectoryName (string path)
105 CheckArgument.Empty (path);
106 CheckArgument.WhitespaceOnly (path);
107 CheckArgument.PathChars (path);
111 int nLast = path.LastIndexOfAny (PathSeparatorChars, path.Length - 2);
115 return path.Substring (0, nLast);
122 public static string GetExtension (string path)
129 CheckArgument.Empty (path);
130 CheckArgument.WhitespaceOnly (path);
132 int iExt = findExtension (path);
133 int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
136 { // okay it has an extension
137 return path.Substring (iExt);
142 public static string GetFileName (string path)
149 CheckArgument.Empty (path);
150 CheckArgument.WhitespaceOnly (path);
152 int nLast = path.LastIndexOfAny (PathSeparatorChars);
156 return path.Substring (nLast + 1);
159 return nLast == 0 ? null : path;
162 public static string GetFileNameWithoutExtension (string path)
164 return ChangeExtension (GetFileName (path), null);
167 public static string GetFullPath (string path)
170 throw (new ArgumentNullException (
172 "You must specify a path when calling System.IO.Path.GetFullPath"));
174 if (path.StartsWith (new string (DirectorySeparatorChar, 1)) ||
175 path.StartsWith (new string (AltDirectorySeparatorChar, 1)))
178 return _os.GetCurrentDirectory () + new string (DirectorySeparatorChar, 1) + path;
181 public static string GetPathRoot (string path)
184 (path.StartsWith (new string (DirectorySeparatorChar, 1)) ||
185 path.StartsWith (new string (AltDirectorySeparatorChar, 1))))
187 return path.Substring (0, 1);
193 public static string GetTempFileName ()
195 //TODO: Implement method
200 /// Returns the path of the current systems temp directory
203 public static string GetTempPath ()
204 { // TODO: This might vary with distribution and there
205 // might be an api to provide it. Research is needed
209 public static bool HasExtension (string path)
211 CheckArgument.Null (path);
212 CheckArgument.Empty (path);
213 CheckArgument.WhitespaceOnly (path);
215 return findExtension (path) > -1;
218 public static bool IsPathRooted (string path)
220 return path.StartsWith (new string (VolumeSeparatorChar,1));
223 // private class methods
225 private static int findExtension (string path)
227 // method should return the index of the path extension
228 // start or -1 if no valid extension
230 int iLastDot = path.LastIndexOf (".");
231 int iLastSep = path.LastIndexOfAny ( PathSeparatorChars );
233 if (iLastDot > iLastSep)