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
48 if (src.IndexOf(':') > 1)
\r
49 throw new NotSupportedException("src");
\r
50 if (dest.IndexOf(':') > 1)
\r
51 throw new NotSupportedException("dest");
\r
52 if (!Exists (src)) {
\r
53 throw new FileNotFoundException (src + " does not exist");
\r
56 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){
\r
57 throw new ArgumentException(src + " is a directory");
\r
60 if (Exists (dest)) {
\r
61 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){
\r
62 throw new ArgumentException(dest + " is a directory");
\r
65 throw new IOException (dest + " already exists");
\r
68 string DirName = Path.GetDirectoryName(dest);
\r
69 if (!Directory.Exists (DirName))
\r
70 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
\r
72 if (!MonoIO.CopyFile (src, dest, overwrite))
\r
73 throw MonoIO.GetException ();
\r
76 public static FileStream Create (string path)
\r
78 return Create (path, 8192);
\r
81 public static FileStream Create (string path, int buffersize)
\r
84 throw new ArgumentNullException("path");
\r
85 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
\r
86 throw new ArgumentException("path");
\r
87 if (path.IndexOf(':') > 1)
\r
88 throw new NotSupportedException();
\r
90 string DirName = Path.GetDirectoryName(path);
\r
91 if (!Directory.Exists (DirName))
\r
92 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
\r
94 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){
\r
95 throw new UnauthorizedAccessException(path + " is a read-only");
\r
99 return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,
\r
100 FileShare.None, buffersize);
\r
103 public static StreamWriter CreateText(string path)
106 return new StreamWriter (path, false);
112 public static void Delete (string path)
\r
115 throw new ArgumentNullException("path");
\r
116 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
\r
117 throw new ArgumentException("path");
\r
118 if (path.IndexOf(':') > 1)
\r
119 throw new NotSupportedException();
\r
120 if (Directory.Exists (path))
\r
121 throw new UnauthorizedAccessException("path is a directory");
\r
123 string DirName = Path.GetDirectoryName(path);
\r
124 if (DirName.Length > 0 && !Directory.Exists (DirName))
\r
125 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
\r
127 if (!MonoIO.DeleteFile (path)){
\r
128 Exception e = MonoIO.GetException ();
\r
129 if (! (e is FileNotFoundException))
\r
134 public static bool Exists (string path)
\r
136 // For security reasons no exceptions are thrown, only false is returned if there
\r
137 // is any problem with the path or permissions. Minimizes what information can be
\r
138 // discovered by using this method.
\r
139 if (null == path || String.Empty == path.Trim()
\r
140 || path.IndexOfAny(Path.InvalidPathChars) >= 0
\r
141 || path.IndexOf(':') > 1)
\r
144 return MonoIO.ExistsFile (path);
\r
147 public static FileAttributes GetAttributes (string path)
\r
150 throw new ArgumentNullException("path");
\r
151 if (String.Empty == path.Trim() || path.IndexOfAny(Path.InvalidPathChars) >= 0)
\r
152 throw new ArgumentException("path");
\r
153 if (path.IndexOf(':') > 1)
\r
154 throw new NotSupportedException();
\r
156 string DirName = Path.GetDirectoryName(path);
\r
157 if (!Directory.Exists(DirName))
\r
158 throw new DirectoryNotFoundException("Directory '" + DirName + "' not found in '" + Environment.CurrentDirectory + "'.");
\r
160 return MonoIO.GetFileAttributes (path);
\r
163 public static DateTime GetCreationTime (string path)
\r
167 MonoIO.GetFileStat (path, out stat);
\r
168 return DateTime.FromFileTime (stat.CreationTime);
\r
171 public static DateTime GetLastAccessTime (string path)
\r
175 MonoIO.GetFileStat (path, out stat);
\r
176 return DateTime.FromFileTime (stat.LastAccessTime);
\r
179 public static DateTime GetLastWriteTime (string path)
\r
183 MonoIO.GetFileStat (path, out stat);
\r
184 return DateTime.FromFileTime (stat.LastWriteTime);
\r
187 public static void Move (string src, string dest)
\r
190 throw new ArgumentNullException ("src");
\r
192 throw new ArgumentNullException ("dest");
\r
193 if (src.Trim () == "" || src.IndexOfAny (Path.InvalidPathChars) != -1)
\r
194 throw new ArgumentException ("src");
\r
195 if (dest.Trim () == "" || dest.IndexOfAny (Path.InvalidPathChars) != -1)
\r
196 throw new ArgumentException ("dest");
\r
197 if (src.IndexOf(':') > 1)
\r
198 throw new NotSupportedException("src");
\r
199 if (dest.IndexOf(':') > 1)
\r
200 throw new NotSupportedException("dest");
\r
202 throw new FileNotFoundException (src + " does not exist");
\r
203 if (Exists (dest) && ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory))
\r
204 throw new ArgumentException(dest + " is a directory");
\r
207 DirName = Path.GetDirectoryName(src);
\r
208 if (!Directory.Exists (DirName))
\r
209 throw new DirectoryNotFoundException("Source directory not found: " + DirName);
\r
210 DirName = Path.GetDirectoryName(dest);
\r
211 if (!Directory.Exists (DirName))
\r
212 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);
\r
214 if (!MonoIO.MoveFile (src, dest))
\r
215 throw MonoIO.GetException ();
\r
218 public static FileStream Open (string path, FileMode mode)
\r
220 return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);
\r
223 public static FileStream Open (string path, FileMode mode, FileAccess access)
\r
225 return new FileStream (path, mode, access, FileShare.None);
\r
228 public static FileStream Open (string path, FileMode mode, FileAccess access,
\r
231 return new FileStream (path, mode, access, share);
\r
234 public static FileStream OpenRead (string path)
\r
236 return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);
\r
239 public static StreamReader OpenText (string path)
\r
241 return new StreamReader (path);
\r
244 public static FileStream OpenWrite (string path)
\r
246 return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
\r
249 public static void SetAttributes (string path, FileAttributes attributes)
\r
251 if (!MonoIO.SetFileAttributes (path, attributes))
\r
252 throw MonoIO.GetException (path);
\r
255 public static void SetCreationTime (string path, DateTime creation_time)
\r
257 if (!MonoIO.SetFileTime (path, creation_time.Ticks, -1, -1))
\r
258 throw MonoIO.GetException (path);
\r
261 public static void SetLastAccessTime (string path, DateTime last_access_time)
\r
263 if (!MonoIO.SetFileTime (path, -1, last_access_time.Ticks, -1))
\r
264 throw MonoIO.GetException (path);
\r
267 public static void SetLastWriteTime (string path, DateTime last_write_time)
\r
269 if (!MonoIO.SetFileTime (path, -1, -1, last_write_time.Ticks))
\r
270 throw MonoIO.GetException (path);
\r