2002-06-17 Dietmar Maurer <dietmar@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / File.cs
1 // \r
2 // System.IO.FIle.cs \r
3 //\r
4 // \r
5 // Authors:\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
9 //\r
10 // Copyright 2002 Ximian, Inc. http://www.ximian.com\r
11 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
12 //\r
13 \r
14 using System;\r
15 \r
16 namespace System.IO\r
17 {\r
18         /// <summary>\r
19         /// \r
20         /// </summary>\r
21         public sealed class File\r
22         {\r
23                 private File () {}
24
25                 \r
26                 \r
27                 public static StreamWriter AppendText (string path)\r
28                 {       \r
29                         return new StreamWriter (path, true);\r
30                 }\r
31 \r
32                 [MonoTODO("Security Permision Checks")]\r
33                 public static void Copy (string sourceFilename, string destFilename)\r
34                 {\r
35                         Copy (sourceFilename, destFilename, false);\r
36                 }\r
37 \r
38                 public static void Copy (string src, string dest, bool overwrite)\r
39                 {       \r
40                         if (src == null)\r
41                                 throw new ArgumentNullException ("src");\r
42                         if (dest == null)\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
54                         }\r
55                         else {\r
56                                 if ((GetAttributes(src) & FileAttributes.Directory) == FileAttributes.Directory){\r
57                                         throw new ArgumentException(src + " is a directory");   \r
58                                 }\r
59                         }\r
60                         if (Exists (dest)) {\r
61                                 if ((GetAttributes(dest) & FileAttributes.Directory) == FileAttributes.Directory){\r
62                                         throw new ArgumentException(dest + " is a directory");  \r
63                                 }\r
64                                 if (!overwrite)\r
65                                         throw new IOException (dest + " already exists");\r
66                         }\r
67 \r
68                         string DirName = Path.GetDirectoryName(dest);\r
69                         if (!Directory.Exists (DirName))\r
70                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
71 \r
72                         if (!MonoIO.CopyFile (src, dest, overwrite))\r
73                                 throw MonoIO.GetException ();\r
74                 }\r
75 \r
76                 public static FileStream Create (string path)\r
77                 {\r
78                         return Create (path, 8192);\r
79                 }\r
80 \r
81                 public static FileStream Create (string path, int buffersize)\r
82                 {\r
83                         if (null == path)\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
89 \r
90                         string DirName = Path.GetDirectoryName(path);\r
91                         if (!Directory.Exists (DirName))\r
92                                 throw new DirectoryNotFoundException("Destination directory not found: " + DirName);\r
93                         if (Exists(path)){\r
94                                 if ((GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly){\r
95                                         throw new UnauthorizedAccessException(path + " is a read-only");        \r
96                                 }\r
97                         }\r
98 \r
99                         return new FileStream (path, FileMode.Create, FileAccess.ReadWrite,\r
100                                                FileShare.None, buffersize);\r
101                 }\r
102
103                 public static StreamWriter CreateText(string path)
104                 \r
105                 {\r
106                         return new StreamWriter (path, false);
107                 \r
108                 }
109                 
110                 \r
111                 \r
112                 public static void Delete (string path)\r
113                 {\r
114                         if (null == 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
122 \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
126 \r
127                         if (!MonoIO.DeleteFile (path)){\r
128                                 Exception e = MonoIO.GetException ();\r
129                                 if (! (e is FileNotFoundException))\r
130                                         throw e;\r
131                         }\r
132                 }\r
133                 \r
134                 public static bool Exists (string path)\r
135                 {\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
142                                 return false;\r
143 \r
144                         return MonoIO.ExistsFile (path);\r
145                 }\r
146 \r
147                 public static FileAttributes GetAttributes (string path)\r
148                 {\r
149                         if (null == 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
155 \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
159 \r
160                         return MonoIO.GetFileAttributes (path);\r
161                 }\r
162 \r
163                 public static DateTime GetCreationTime (string path)\r
164                 {\r
165                         MonoIOStat stat;\r
166 \r
167                         MonoIO.GetFileStat (path, out stat);\r
168                         return DateTime.FromFileTime (stat.CreationTime);\r
169                 }\r
170 \r
171                 public static DateTime GetLastAccessTime (string path)\r
172                 {\r
173                         MonoIOStat stat;\r
174 \r
175                         MonoIO.GetFileStat (path, out stat);\r
176                         return DateTime.FromFileTime (stat.LastAccessTime);\r
177                 }\r
178 \r
179                 public static DateTime GetLastWriteTime (string path)\r
180                 {\r
181                         MonoIOStat stat;\r
182 \r
183                         MonoIO.GetFileStat (path, out stat);\r
184                         return DateTime.FromFileTime (stat.LastWriteTime);\r
185                 }\r
186 \r
187                 public static void Move (string src, string dest)\r
188                 {\r
189                         if (src == null)\r
190                                 throw new ArgumentNullException ("src");\r
191                         if (dest == null)\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
201                         if (!Exists (src))\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
205 \r
206                         string DirName;\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
213 \r
214                         if (!MonoIO.MoveFile (src, dest))\r
215                                 throw MonoIO.GetException ();\r
216                 }\r
217                 \r
218                 public static FileStream Open (string path, FileMode mode)\r
219                 {       \r
220                         return new FileStream (path, mode, FileAccess.ReadWrite, FileShare.None);\r
221                 }\r
222                 \r
223                 public static FileStream Open (string path, FileMode mode, FileAccess access)\r
224                 {       \r
225                         return new FileStream (path, mode, access, FileShare.None);\r
226                 }\r
227 \r
228                 public static FileStream Open (string path, FileMode mode, FileAccess access,\r
229                                                FileShare share)\r
230                 {\r
231                         return new FileStream (path, mode, access, share);\r
232                 }\r
233                 \r
234                 public static FileStream OpenRead (string path)\r
235                 {       \r
236                         return new FileStream (path, FileMode.Open, FileAccess.Read, FileShare.Read);\r
237                 }\r
238 \r
239                 public static StreamReader OpenText (string path)\r
240                 {\r
241                         return new StreamReader (path);\r
242                 }\r
243 \r
244                 public static FileStream OpenWrite (string path)\r
245                 {\r
246                         return new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);\r
247                 }\r
248 \r
249                 public static void SetAttributes (string path, FileAttributes attributes)\r
250                 {\r
251                         if (!MonoIO.SetFileAttributes (path, attributes))\r
252                                 throw MonoIO.GetException (path);\r
253                 }\r
254 \r
255                 public static void SetCreationTime (string path, DateTime creation_time)\r
256                 {\r
257                         if (!MonoIO.SetFileTime (path, creation_time.Ticks, -1, -1))\r
258                                 throw MonoIO.GetException (path);\r
259                 }\r
260 \r
261                 public static void SetLastAccessTime (string path, DateTime last_access_time)\r
262                 {\r
263                         if (!MonoIO.SetFileTime (path, -1, last_access_time.Ticks, -1))\r
264                                 throw MonoIO.GetException (path);\r
265                 }\r
266 \r
267                 public static void SetLastWriteTime (string path, DateTime last_write_time)\r
268                 {\r
269                         if (!MonoIO.SetFileTime (path, -1, -1, last_write_time.Ticks))\r
270                                 throw MonoIO.GetException (path);\r
271                 }\r
272         }\r
273 }\r