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 //------------------------------------------------------------------------------
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 /// A utility class to assist with various argument validations in System.IO
46 internal sealed class CheckArgument
49 /// Generates and exception if arg contains whitepace only
51 public static void WhitespaceOnly (string arg
, string desc
)
53 if (arg
!= null && arg
.Length
> 0)
55 string temp
= arg
.Trim ();
58 throw new ArgumentException (desc
);
64 /// Generates and exception if arg contains whitepace only
66 public static void WhitespaceOnly (string arg
)
68 WhitespaceOnly (arg
, "Argument string consists of whitespace characters only.");
72 /// Generates and exception if arg is empty
74 public static void Empty (string arg
, string desc
)
76 if (arg
!= null && arg
.Length
== 0)
78 throw new ArgumentException (desc
);
83 /// Generates and exception if arg is empty
85 public static void Empty (string arg
)
87 Empty (arg
, "Argument string is empty.");
91 /// Generates and exception if arg is null
93 public static void Null (Object arg
, string desc
)
97 throw new ArgumentNullException (desc
);
102 /// Generates and exception if arg is null
104 public static void Null (Object arg
)
108 throw new ArgumentNullException ();
113 /// Generates and exception if path contains invalid path characters
115 public static void PathChars (string path
, string desc
)
119 if (path
.IndexOfAny (System
.IO
.Path
.InvalidPathChars
) > -1)
121 throw new ArgumentException (desc
);
127 /// Generates and exception if path contains invalid path characters
129 public static void PathChars (string path
)
131 PathChars (path
, "Path contains invalid characters");
135 /// Generates and exception if path too long
138 public static void PathLength (string path
, string desc
)
140 //TODO: find out how long is too long
144 /// Generates and exception if path too long
146 public static void PathLength (string path
)
148 PathLength (path
, "Path is too long");
152 /// Generates and exception if path is illegal
154 public static void Path (string path
, bool bAllowNull
, bool bLength
)
156 if (path
!= null) //allow null
158 Empty (path
, "Path cannot be the empty string"); // path can't be empty
159 WhitespaceOnly (path
, "Path cannot be all whitespace"); // path can't be all whitespace
160 PathChars (path
); // path can't contain invalid characters
163 PathLength ("Path too long");
166 else if (!bAllowNull
)
168 throw new ArgumentNullException ("Parameter name: path");
173 /// Generates and exception if path is illegal
175 public static void Path (string path
, bool bAllowNull
)
177 Path (path
, bAllowNull
, false);
181 /// Generates and exception if path is illegal
183 public static void Path (string path
)
185 Path (path
, false, false);
189 } // namespace System.IO.Private