2 // System.IO.MonoIO.cs: static interface to native filesystem.
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Dick Porter (dick@ximian.com)
12 using System.Runtime.CompilerServices;
16 internal sealed class MonoIO {
17 public static readonly FileAttributes
18 InvalidFileAttributes = (FileAttributes)(-1);
20 public static readonly IntPtr
21 InvalidHandle = (IntPtr)(-1L);
25 public static Exception GetException (MonoIOError error)
27 return GetException (String.Empty, error);
30 public static Exception GetException (string path,
36 // FIXME: add more exception mappings here
37 case MonoIOError.ERROR_FILE_NOT_FOUND:
38 message = String.Format ("Could not find file \"{0}\"", path);
39 return new FileNotFoundException (message);
41 case MonoIOError.ERROR_PATH_NOT_FOUND:
42 message = String.Format ("Could not find a part of the path \"{0}\"", path);
43 return new DirectoryNotFoundException (message);
45 case MonoIOError.ERROR_ACCESS_DENIED:
46 message = String.Format ("Access to the path \"{0}\" is denied.", path);
47 return new UnauthorizedAccessException (message);
50 message = String.Format ("Win32 IO returned {0}", error);
51 return new IOException (message);
57 [MethodImplAttribute (MethodImplOptions.InternalCall)]
58 public extern static bool CreateDirectory (string path, out MonoIOError error);
60 [MethodImplAttribute (MethodImplOptions.InternalCall)]
61 public extern static bool RemoveDirectory (string path, out MonoIOError error);
63 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat, out MonoIOError error);
66 [MethodImplAttribute (MethodImplOptions.InternalCall)]
67 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat, out MonoIOError error);
69 [MethodImplAttribute (MethodImplOptions.InternalCall)]
70 public extern static bool FindClose (IntPtr find,
71 out MonoIOError error);
73 [MethodImplAttribute (MethodImplOptions.InternalCall)]
74 public extern static string GetCurrentDirectory (out MonoIOError error);
76 [MethodImplAttribute (MethodImplOptions.InternalCall)]
77 public extern static bool SetCurrentDirectory (string path, out MonoIOError error);
81 [MethodImplAttribute (MethodImplOptions.InternalCall)]
82 public extern static bool MoveFile (string path, string dest,
83 out MonoIOError error);
85 [MethodImplAttribute (MethodImplOptions.InternalCall)]
86 public extern static bool CopyFile (string path, string dest,
88 out MonoIOError error);
90 [MethodImplAttribute (MethodImplOptions.InternalCall)]
91 public extern static bool DeleteFile (string path,
92 out MonoIOError error);
94 [MethodImplAttribute (MethodImplOptions.InternalCall)]
95 public extern static FileAttributes GetFileAttributes (string path, out MonoIOError error);
97 [MethodImplAttribute (MethodImplOptions.InternalCall)]
98 public extern static bool SetFileAttributes (string path, FileAttributes attrs, out MonoIOError error);
100 [MethodImplAttribute (MethodImplOptions.InternalCall)]
101 public extern static MonoFileType GetFileType (IntPtr handle, out MonoIOError error);
103 public static bool Exists (string path, out MonoIOError error)
105 FileAttributes attrs = GetFileAttributes (path,
107 if (attrs == InvalidFileAttributes)
113 public static bool ExistsFile (string path,
114 out MonoIOError error)
116 FileAttributes attrs = GetFileAttributes (path,
118 if (attrs == InvalidFileAttributes)
121 if ((attrs & FileAttributes.Directory) != 0)
127 public static bool ExistsDirectory (string path,
128 out MonoIOError error)
130 FileAttributes attrs = GetFileAttributes (path,
132 if (attrs == InvalidFileAttributes)
135 if ((attrs & FileAttributes.Directory) == 0)
141 [MethodImplAttribute (MethodImplOptions.InternalCall)]
142 public extern static bool GetFileStat (string path,
144 out MonoIOError error);
148 [MethodImplAttribute (MethodImplOptions.InternalCall)]
149 public extern static IntPtr Open (string filename,
153 out MonoIOError error);
155 [MethodImplAttribute (MethodImplOptions.InternalCall)]
156 public extern static bool Close (IntPtr handle,
157 out MonoIOError error);
159 [MethodImplAttribute (MethodImplOptions.InternalCall)]
160 public extern static int Read (IntPtr handle, byte [] dest,
161 int dest_offset, int count,
162 out MonoIOError error);
164 [MethodImplAttribute (MethodImplOptions.InternalCall)]
165 public extern static int Write (IntPtr handle, byte [] src,
166 int src_offset, int count,
167 out MonoIOError error);
169 [MethodImplAttribute (MethodImplOptions.InternalCall)]
170 public extern static long Seek (IntPtr handle, long offset,
172 out MonoIOError error);
174 [MethodImplAttribute (MethodImplOptions.InternalCall)]
175 public extern static bool Flush (IntPtr handle,
176 out MonoIOError error);
178 [MethodImplAttribute (MethodImplOptions.InternalCall)]
179 public extern static long GetLength (IntPtr handle,
180 out MonoIOError error);
182 [MethodImplAttribute (MethodImplOptions.InternalCall)]
183 public extern static bool SetLength (IntPtr handle,
185 out MonoIOError error);
187 [MethodImplAttribute (MethodImplOptions.InternalCall)]
188 public extern static bool SetFileTime (IntPtr handle,
190 long last_access_time,
191 long last_write_time,
192 out MonoIOError error);
194 public static bool SetFileTime (string path,
196 long last_access_time,
197 long last_write_time,
198 out MonoIOError error)
203 handle = Open (path, FileMode.Open,
204 FileAccess.ReadWrite,
205 FileShare.ReadWrite, out error);
206 if (handle == IntPtr.Zero)
209 result = SetFileTime (handle, creation_time,
211 last_write_time, out error);
212 Close (handle, out error);
219 public extern static IntPtr ConsoleOutput {
220 [MethodImplAttribute (MethodImplOptions.InternalCall)]
224 public extern static IntPtr ConsoleInput {
225 [MethodImplAttribute (MethodImplOptions.InternalCall)]
229 public extern static IntPtr ConsoleError {
230 [MethodImplAttribute (MethodImplOptions.InternalCall)]
236 [MethodImplAttribute (MethodImplOptions.InternalCall)]
237 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
241 public extern static char VolumeSeparatorChar {
242 [MethodImplAttribute (MethodImplOptions.InternalCall)]
246 public extern static char DirectorySeparatorChar {
247 [MethodImplAttribute (MethodImplOptions.InternalCall)]
251 public extern static char AltDirectorySeparatorChar {
252 [MethodImplAttribute (MethodImplOptions.InternalCall)]
256 public extern static char PathSeparator {
257 [MethodImplAttribute (MethodImplOptions.InternalCall)]
261 public extern static char [] InvalidPathChars {
262 [MethodImplAttribute (MethodImplOptions.InternalCall)]
266 [MethodImplAttribute (MethodImplOptions.InternalCall)]
267 public extern static int GetTempPath(out string path);