2 // System.IO.FIle.cs
\r
6 // Miguel de Icaza (miguel@ximian.com)
\r
7 // Jim Richardson (develop@wtfo-guru.com)
\r
8 // Dan Lewis (dihlewis@yahoo.co.uk)
\r
10 // Copyright 2002 Ximian, Inc. http://www.ximian.com
\r
11 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved
\r
21 public sealed class File
\r
27 public static StreamWriter AppendText (string path)
\r
29 return new StreamWriter (path, true);
\r
32 [MonoTODO("Security Permision Checks")]
\r
33 public static void Copy (string sourceFilename, string destFilename)
\r
35 Copy (sourceFilename, destFilename, false);
\r
38 public static void Copy (string src, string dest, bool overwrite)
\r
41 throw new ArgumentNullException ("src");
\r
43 throw new ArgumentNullException ("dest");
\r
44 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
\r
45 throw new ArgumentException ("src");
\r
46 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
\r
47 throw new ArgumentException ("dest");
\r
49 throw new FileNotFoundException (src + " does not exist");
\r
51 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
52 throw new ArgumentException(src + " is a directory");
55 if (Exists (dest)) {
\r
56 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
\r
57 throw new ArgumentException(dest + " is a directory");
\r
60 throw new IOException (dest + " already exists");
\r
63 string DirName = Path.GetDirectoryName(dest);
\r
64 if (DirName != String.Empty && !Directory.Exists (DirName))
65 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
69 if (!MonoIO.CopyFile (src, dest, overwrite, out error))
70 throw MonoIO.GetException (error);
73 public static FileStream Create (string path)
\r
75 return Create (path, 8192);
\r
78 public static FileStream Create (string path, int buffersize)
\r
81 throw new ArgumentNullException("path");
\r
82 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
\r
83 throw new ArgumentException("path");
\r
85 string DirName = Path.GetDirectoryName(path);
\r
86 if (DirName != String.Empty && !Directory.Exists (DirName))
\r
87 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
\r
89 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
\r
90 throw new UnauthorizedAccessException(path + " is a read-only");
\r
94 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
\r
95 FileShare.None, buffersize);
\r
98 public static StreamWriter CreateText(string path)
101 return new StreamWriter (path, false);
107 public static void Delete (string path)
\r
110 throw new ArgumentNullException("path");
\r
111 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
\r
112 throw new ArgumentException("path");
\r
113 if (Directory.Exists (path))
\r
114 throw new UnauthorizedAccessException("path is a directory");
\r
116 string DirName = Path.GetDirectoryName(path);
\r
117 if (DirName != String.Empty && !Directory.Exists (DirName))
\r
118 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
\r
122 if (!MonoIO.DeleteFile (path, out error)){
123 Exception e = MonoIO.GetException (error);
124 if (! (e is FileNotFoundException))
129 public static bool Exists (string path)
131 // For security reasons no exceptions are
132 // thrown, only false is returned if there is
133 // any problem with the path or permissions.
134 // Minimizes what information can be
135 // discovered by using this method.
136 if (null == path || String.Empty == path.Trim()
137 || path.IndexOfAny(Path.InvalidPathChars) >= 0) {
143 return MonoIO.ExistsFile (path, out error);
146 public static FileAttributes GetAttributes (string path)
149 throw new ArgumentNullException("path");
152 if (String.Empty == path.Trim()) {
153 throw new ArgumentException("Path is empty");
156 if (path.IndexOfAny(Path.InvalidPathChars) >= 0) {
157 throw new ArgumentException("Path contains invalid chars");
162 return MonoIO.GetFileAttributes (path, out error);
165 public static DateTime GetCreationTime (string path)
170 if (!MonoIO.GetFileStat (path, out stat, out error))
171 throw new IOException (path);
172 return DateTime.FromFileTime (stat.CreationTime);
175 public static DateTime GetLastAccessTime (string path)
180 if (!MonoIO.GetFileStat (path, out stat, out error))
181 throw MonoIO.GetException (path, error);
182 return DateTime.FromFileTime (stat.LastAccessTime);
185 public static DateTime GetLastWriteTime (string path)
190 if (!MonoIO.GetFileStat (path, out stat, out error))
191 throw MonoIO.GetException (path, error);
192 return DateTime.FromFileTime (stat.LastWriteTime);
195 public static void Move (string src, string dest)
\r
198 throw new ArgumentNullException ("src");
\r
200 throw new ArgumentNullException ("dest");
\r
201 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
\r
202 throw new ArgumentException ("src");
\r
203 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
\r
204 throw new ArgumentException ("dest");
\r
206 throw new FileNotFoundException (src + " does not exist");
\r
207 if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))
\r
208 throw new ArgumentException(dest + " is a directory");
\r
211 DirName = Path.GetDirectoryName(src);
\r
212 if (DirName != String.Empty && !Directory.Exists (DirName))
\r
213 throw new DirectoryNotFoundException("Source directory not found: " + DirName);
\r
214 DirName = Path.GetDirectoryName(dest);
\r
215 if (DirName != String.Empty && !Directory.Exists (DirName))
\r
216 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
\r
220 if (!MonoIO.MoveFile (src, dest, out error))
221 throw MonoIO.GetException (error);
224 public static FileStream Open (string path, FileMode mode)
\r
226 return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
\r
229 public static FileStream Open (string path, FileMode mode, FileAccess access)
\r
231 return new FileStream (path, mode, access, FileShare.None);
\r
234 public static FileStream Open (string path, FileMode mode, FileAccess access,
\r
237 return new FileStream (path, mode, access, share);
\r
240 public static FileStream OpenRead (string path)
\r
242 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
245 public static StreamReader OpenText (string path)
\r
247 return new StreamReader (path);
\r
250 public static FileStream OpenWrite (string path)
\r
252 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
\r
255 public static void SetAttributes (string path,
256 FileAttributes attributes)
260 if (!MonoIO.SetFileAttributes (path, attributes,
262 throw MonoIO.GetException (path, error);
266 public static void SetCreationTime (string path,
267 DateTime creation_time)
271 if (!MonoIO.SetFileTime (path, creation_time.Ticks,
272 -1, -1, out error)) {
273 throw MonoIO.GetException (path, error);
277 public static void SetLastAccessTime (string path,DateTime last_access_time)
281 if (!MonoIO.SetFileTime (path, -1,
282 last_access_time.Ticks, -1,
284 throw MonoIO.GetException (path, error);
288 public static void SetLastWriteTime (string path,
289 DateTime last_write_time)
293 if (!MonoIO.SetFileTime (path, -1, -1,
294 last_write_time.Ticks,
296 throw MonoIO.GetException (path, error);