1 //------------------------------------------------------------------------------
3 // System.IO.CheckArgument.cs
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
7 // Author: Jim Richardson, develop@wtfo-guru.com
8 // Created: Saturday, August 25, 2001
10 // NOTE: All contributors can freely add to this class or make modifications
11 // that do not break existing usage of methods
12 //------------------------------------------------------------------------------
21 /// A utility class to assist with various argument validations in System.IO
23 internal sealed class CheckArgument
26 /// Generates and exception if arg contains whitepace only
28 public static void WhitespaceOnly (string arg, string desc)
30 if (arg != null && arg.Length > 0)
36 throw new ArgumentException (desc);
42 /// Generates and exception if arg contains whitepace only
44 public static void WhitespaceOnly (string arg)
46 WhitespaceOnly (arg, "Argument string consists of whitespace characters only.");
50 /// Generates and exception if arg is empty
52 public static void Empty (string arg, string desc)
54 if (arg != null && arg.Length == 0)
56 throw new ArgumentException (desc);
61 /// Generates and exception if arg is empty
63 public static void Empty (string arg)
65 Empty (arg, "Argument string is empty.");
69 /// Generates and exception if arg is null
71 public static void Null (Object arg, string desc)
75 throw new ArgumentNullException (desc);
80 /// Generates and exception if arg is null
82 public static void Null (Object arg)
86 throw new ArgumentNullException ();
91 /// Generates and exception if path contains invalid path characters
93 public static void PathChars (string path, string desc)
97 if (path.IndexOfAny (System.IO.Path.InvalidPathChars) > -1)
99 throw new ArgumentException (desc);
105 /// Generates and exception if path contains invalid path characters
107 public static void PathChars (string path)
109 PathChars (path, "Path contains invalid characters");
113 /// Generates and exception if path too long
116 public static void PathLength (string path, string desc)
118 //TODO: find out how long is too long
122 /// Generates and exception if path too long
124 public static void PathLength (string path)
130 /// Generates and exception if path is illegal
132 public static void Path (string path, bool bAllowNull, bool bLength)
134 if (path != null) //allow null
136 Empty (path, "Path cannot be the empty string"); // path can't be empty
137 WhitespaceOnly (path, "Path cannot be all whitespace"); // path can't be all whitespace
138 PathChars (path); // path can't contain invalid characters
141 PathLength ("Path too long");
144 else if (!bAllowNull)
146 throw new ArgumentNullException ("Parameter name: path");
151 /// Generates and exception if path is illegal
153 public static void Path (string path, bool bAllowNull)
155 Path (path, bAllowNull, false);
159 /// Generates and exception if path is illegal
161 public static void Path (string path)
163 Path (path, false, false);
167 } // namespace System.IO.Private