(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.IO / File.cs
blob05cf0ed426b30f32b13e33f815d7b3f32222b858
1 //
2 // System.IO.FIle.cs
3 //
4 //
5 // Authors:
6 // Miguel de Icaza (miguel@ximian.com)
7 // Jim Richardson (develop@wtfo-guru.com)
8 // Dan Lewis (dihlewis@yahoo.co.uk)
9 // Ville Palo (vi64pa@kolumbus.fi)
11 // Copyright 2002 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
25 //
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
28 //
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System;
40 namespace System.IO
42 /// <summary>
43 ///
44 /// </summary>
45 public
46 #if NET_2_0
47 static
48 #else
49 sealed
50 #endif
51 class File
54 #if !NET_2_0
55 private File () {}
56 #endif
58 public static StreamWriter AppendText (string path)
60 return new StreamWriter (path, true);
63 [MonoTODO("Security Permision Checks")]
64 public static void Copy (string sourceFilename, string destFilename)
66 Copy (sourceFilename, destFilename, false);
69 public static void Copy (string src, string dest, bool overwrite)
71 if (src == null)
72 throw new ArgumentNullException ("src");
73 if (dest == null)
74 throw new ArgumentNullException ("dest");
75 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
76 throw new ArgumentException (Locale.GetText ("src is null"));
77 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
78 throw new ArgumentException (Locale.GetText ("dest is empty or contains invalid characters"));
79 if (!Exists (src))
80 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
82 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
83 throw new ArgumentException(Locale.GetText ("{0} is a directory", src));
86 if (Exists (dest)) {
87 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
88 throw new ArgumentException (Locale.GetText ("{0} is a directory", dest));
90 if (!overwrite)
91 throw new IOException (Locale.GetText ("{0} already exists", dest));
94 string DirName = Path.GetDirectoryName(dest);
95 if (DirName != String.Empty && !Directory.Exists (DirName))
96 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}",DirName));
98 MonoIOError error;
100 if (!MonoIO.CopyFile (src, dest, overwrite, out error)){
101 string p = Locale.GetText ("{0}\" or \"{1}", src, dest);
102 throw MonoIO.GetException (p, error);
106 public static FileStream Create (string path)
108 return Create (path, 8192);
111 public static FileStream Create (string path, int buffersize)
113 if (null == path)
114 throw new ArgumentNullException ("path");
115 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
116 throw new ArgumentException (Locale.GetText ("path is invalid"));
118 string DirName = Path.GetDirectoryName(path);
119 if (DirName != String.Empty && !Directory.Exists (DirName))
120 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
121 if (Exists(path)){
122 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
123 throw new UnauthorizedAccessException (Locale.GetText ("{0} is read-only", path));
127 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
128 FileShare.None, buffersize);
131 public static StreamWriter CreateText(string path)
134 return new StreamWriter (path, false);
140 public static void Delete (string path)
142 if (null == path)
143 throw new ArgumentNullException("path");
144 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
145 throw new ArgumentException("path");
146 if (Directory.Exists (path))
147 throw new UnauthorizedAccessException(Locale.GetText ("{0} is a directory", path));
149 string DirName = Path.GetDirectoryName(path);
150 if (DirName != String.Empty && !Directory.Exists (DirName))
151 throw new DirectoryNotFoundException (Locale.GetText ("Destination directory not found: {0}", DirName));
153 MonoIOError error;
155 if (!MonoIO.DeleteFile (path, out error)){
156 Exception e = MonoIO.GetException (path, error);
157 if (! (e is FileNotFoundException))
158 throw e;
162 public static bool Exists (string path)
164 // For security reasons no exceptions are
165 // thrown, only false is returned if there is
166 // any problem with the path or permissions.
167 // Minimizes what information can be
168 // discovered by using this method.
169 if (null == path || String.Empty == path.Trim()
170 || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
171 return false;
174 MonoIOError error;
175 bool exists;
177 exists = MonoIO.ExistsFile (path, out error);
178 if (error != MonoIOError.ERROR_SUCCESS &&
179 error != MonoIOError.ERROR_FILE_NOT_FOUND &&
180 error != MonoIOError.ERROR_PATH_NOT_FOUND) {
181 throw MonoIO.GetException (path, error);
184 return(exists);
187 public static FileAttributes GetAttributes (string path)
189 if (null == path) {
190 throw new ArgumentNullException("path");
193 if (String.Empty == path.Trim()) {
194 throw new ArgumentException (Locale.GetText ("Path is empty"));
197 if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
198 throw new ArgumentException(Locale.GetText ("Path contains invalid chars"));
201 MonoIOError error;
202 FileAttributes attrs;
204 attrs = MonoIO.GetFileAttributes (path, out error);
205 if (error != MonoIOError.ERROR_SUCCESS) {
206 throw MonoIO.GetException (path, error);
209 return(attrs);
212 public static DateTime GetCreationTime (string path)
214 MonoIOStat stat;
215 MonoIOError error;
216 CheckPathExceptions (path);
218 if (!MonoIO.GetFileStat (path, out stat, out error))
219 throw new IOException (path);
220 return DateTime.FromFileTime (stat.CreationTime);
223 public static DateTime GetCreationTimeUtc (string path)
225 return GetCreationTime (path).ToUniversalTime ();
228 public static DateTime GetLastAccessTime (string path)
230 MonoIOStat stat;
231 MonoIOError error;
232 CheckPathExceptions (path);
234 if (!MonoIO.GetFileStat (path, out stat, out error))
235 throw new IOException (path);
236 return DateTime.FromFileTime (stat.LastAccessTime);
239 public static DateTime GetLastAccessTimeUtc (string path)
241 return GetLastAccessTime (path).ToUniversalTime ();
244 public static DateTime GetLastWriteTime (string path)
246 MonoIOStat stat;
247 MonoIOError error;
248 CheckPathExceptions (path);
250 if (!MonoIO.GetFileStat (path, out stat, out error))
251 throw new IOException (path);
252 return DateTime.FromFileTime (stat.LastWriteTime);
255 public static DateTime GetLastWriteTimeUtc (string path)
257 return GetLastWriteTime (path).ToUniversalTime ();
260 public static void Move (string src, string dest)
262 MonoIOError error;
264 if (src == null)
265 throw new ArgumentNullException ("src");
266 if (dest == null)
267 throw new ArgumentNullException ("dest");
268 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
269 throw new ArgumentException ("src");
270 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
271 throw new ArgumentException ("dest");
272 if (!MonoIO.Exists (src, out error))
273 throw new FileNotFoundException (Locale.GetText ("{0} does not exist", src), src);
274 if (MonoIO.ExistsDirectory (dest, out error))
275 throw new IOException (Locale.GetText ("{0} is a directory", dest));
276 if (MonoIO.Exists (dest, out error))
277 throw new IOException (Locale.GetText ("{0} already exists", dest));
279 string DirName;
280 DirName = Path.GetDirectoryName(src);
281 if (DirName != String.Empty && !Directory.Exists (DirName))
282 throw new DirectoryNotFoundException(Locale.GetText ("Source directory not found: {0}", DirName));
283 DirName = Path.GetDirectoryName(dest);
284 if (DirName != String.Empty && !Directory.Exists (DirName))
285 throw new DirectoryNotFoundException(Locale.GetText ("Destination directory not found: {0}", DirName));
287 if (!MonoIO.MoveFile (src, dest, out error))
288 throw MonoIO.GetException (error);
291 public static FileStream Open (string path, FileMode mode)
293 return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
296 public static FileStream Open (string path, FileMode mode, FileAccess access)
298 return new FileStream (path, mode, access, FileShare.None);
301 public static FileStream Open (string path, FileMode mode, FileAccess access,
302 FileShare share)
304 return new FileStream (path, mode, access, share);
307 public static FileStream OpenRead (string path)
309 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
312 public static StreamReader OpenText (string path)
314 return new StreamReader (path);
317 public static FileStream OpenWrite (string path)
319 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
322 public static void SetAttributes (string path,
323 FileAttributes attributes)
325 MonoIOError error;
326 CheckPathExceptions (path);
328 if (!MonoIO.SetFileAttributes (path, attributes,
329 out error)) {
330 throw MonoIO.GetException (path, error);
334 public static void SetCreationTime (string path,
335 DateTime creation_time)
337 MonoIOError error;
338 CheckPathExceptions (path);
339 if (!MonoIO.Exists (path, out error))
340 throw MonoIO.GetException (path, error);
342 if (!MonoIO.SetCreationTime (path, creation_time, out error)) {
343 throw MonoIO.GetException (path, error);
347 public static void SetCreationTimeUtc (string path,
348 DateTime creation_time)
350 SetCreationTime (path, creation_time.ToLocalTime ());
353 public static void SetLastAccessTime (string path,DateTime last_access_time)
355 MonoIOError error;
356 CheckPathExceptions (path);
357 if (!MonoIO.Exists (path, out error))
358 throw MonoIO.GetException (path, error);
360 if (!MonoIO.SetLastAccessTime (path, last_access_time, out error)) {
361 throw MonoIO.GetException (path, error);
365 public static void SetLastAccessTimeUtc (string path,DateTime last_access_time)
367 SetLastAccessTime (path, last_access_time.ToLocalTime ());
370 public static void SetLastWriteTime (string path,
371 DateTime last_write_time)
373 MonoIOError error;
374 CheckPathExceptions (path);
375 if (!MonoIO.Exists (path, out error))
376 throw MonoIO.GetException (path, error);
378 if (!MonoIO.SetLastWriteTime (path, last_write_time, out error)) {
379 throw MonoIO.GetException (path, error);
383 public static void SetLastWriteTimeUtc (string path,
384 DateTime last_write_time)
386 SetLastWriteTime (path, last_write_time.ToLocalTime ());
389 #region Private
391 private static void CheckPathExceptions (string path)
393 if (path == null)
394 throw new System.ArgumentNullException("path");
395 if (path == "")
396 throw new System.ArgumentException(Locale.GetText ("Path is empty"));
397 if (path.Trim().Length == 0)
398 throw new ArgumentException (Locale.GetText ("Path is empty"));
399 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
400 throw new ArgumentException (Locale.GetText ("Path contains invalid chars"));
403 #endregion