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 //------------------------------------------------------------------------------
16 public class Path : Object
18 public static readonly char AltDirectorySeparatorChar = PlatformSpecific.InvalidPathChars;
19 public static readonly char DirectorySeparatorChar = PlatformSpecific.InvalidPathChars;
20 public static readonly char[] InvalidPathChars = PlatformSpecific.InvalidPathChars;
21 public static readonly char PathSeparator = PlatformSpecific.InvalidPathChars;
22 public static readonly char VolumeSeparatorChar = PlatformSpecific.InvalidPathChars;
24 private static readonly char[] PathSeparatorChars = { DirectorySeparatorChar,
25 AltDirectorySeparatorChar,
26 VolumeSeparatorChar };
29 public static string ChangeExtension(string path, string extension)
36 int iExt = findExtension(path);
40 return extension == null ? path : path + extension;
44 string temp = path.Substring(0, iExt);
47 return temp + extension;
55 public static string Combine(string path1, string path2)
57 if(path1 == null || path2 == null)
64 // TODO: Check for invalid DirectoryInfo characters
65 // although I don't think it is necesary for linux
67 // TODO: Verify functionality further after NUnit tests written
68 // since the documentation was rather sketchy
70 if(IsPathRooted(path2))
72 if(path1.Equals(string.Empty))
76 throw new ArgumentException("Rooted path");
79 string dirSep = new string(DirectorySeparatorChar, 1);
80 string altSep = new string(AltDirectorySeparatorChar, 1);
82 bool b1 = path1.EndsWith(dirSep) || path1.EndsWith(dirSep);
83 bool b2 = path2.StartsWith(dirSep) || path2.StartsWith(altSep);
86 throw new ArgumentException("Invalid combination");
91 return path1 + dirSep + path2;
97 public static string GetDirectoryName(string path)
104 throwWhiteSpaceOnlyIf(path);
105 throwInvalidPathCharsIf(path);
109 int nLast = path.LastIndexOfAny(PathSeparatorChars, path.Length - 2);
113 return path.Substring(0, nLast);
119 public static string GetExtension(string path)
127 throwWhiteSpaceOnlyIf(path);
129 int iExt = findExtension(path);
130 int iLastSep = path.LastIndexOfAny( PathSeparatorChars );
133 { // okay it has an extension
134 return path.Substring(iExt);
139 public static string GetFileName(string path)
147 throwWhiteSpaceOnlyIf(path);
149 int nLast = path.LastIndexOfAny(PathSeparatorChars);
153 return path.Substring(nLast + 1);
156 return nLast == 0 ? null : path;
159 public static string GetFileNameWithoutExtension(string path)
161 return ChangeExtension(GetFileName(path), null);
164 public static string GetFullPath(string path)
168 //TODO: Implement this correctly
174 public static string GetPathRoot(string path)
177 (path.StartsWith(new string(DirectorySeparatorChar, 1)) ||
178 path.StartsWith(new string(AltDirectorySeparatorChar, 1))))
180 return path.Substring(0, 1);
185 public static string GetTempFileName()
187 //TODO: Implement method
192 /// Returns the path of the current systems temp directory
194 public static string GetTempPath()
195 { // TODO: This might vary with distribution and there
196 // might be an api to provide it. Research is needed
200 public static bool HasExtension(string path)
204 throwWhiteSpaceOnlyIf(path);
206 return findExtension(path) > -1;
209 public static bool IsPathRooted(string path)
211 return path.StartsWith(new string(VolumeSeparatorChar,1));
214 // private class methods
216 private static int findExtension(string path)
217 { // method should return the index of the path extension
218 // start or -1 if no valid extension
221 int iLastDot = path.LastIndexOf(".");
222 int iLastSep = path.LastIndexOfAny( PathSeparatorChars );
224 if(iLastDot > iLastSep)
232 private static void throwNullIf(string path)
236 throw new ArgumentNullException();
240 private static void throwEmptyIf(string path)
242 if(path != null && path.Length == 0)
244 throw new ArgumentException("Empty string");
248 private static void throwWhiteSpaceOnlyIf(string path)
256 throw new ArgumentException("Whitespace only string");
261 private static void throwInvalidPathCharsIf(string path)
263 if(path != null && path.IndexOfAny(InvalidPathChars) > -1)
265 throw new ArgumentException("Invalid path characters");