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)(-1);
25 [MethodImplAttribute (MethodImplOptions.InternalCall)]
26 public extern static MonoIOError GetLastError ();
28 public static Exception GetException ()
30 return GetException (String.Empty);
33 public static Exception GetException (string path)
35 MonoIOError error = GetLastError ();
39 // FIXME: add more exception mappings here
40 case MonoIOError.ERROR_FILE_NOT_FOUND:
41 message = String.Format ("Could not find file \"{0}\"", path);
42 return new FileNotFoundException (message);
44 case MonoIOError.ERROR_PATH_NOT_FOUND:
45 message = String.Format ("Could not find a part of the path \"{0}\"", path);
46 return new DirectoryNotFoundException (message);
48 case MonoIOError.ERROR_ACCESS_DENIED:
49 message = String.Format ("Access to the path \"{0}\" is denied.", path);
50 return new UnauthorizedAccessException (message);
53 message = String.Format ("Win32 IO returned {0}", error);
54 return new IOException (message);
60 [MethodImplAttribute (MethodImplOptions.InternalCall)]
61 public extern static bool CreateDirectory (string path);
63 [MethodImplAttribute (MethodImplOptions.InternalCall)]
64 public extern static bool RemoveDirectory (string path);
66 [MethodImplAttribute (MethodImplOptions.InternalCall)]
67 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat);
69 [MethodImplAttribute (MethodImplOptions.InternalCall)]
70 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat);
72 [MethodImplAttribute (MethodImplOptions.InternalCall)]
73 public extern static bool FindClose (IntPtr find);
75 [MethodImplAttribute (MethodImplOptions.InternalCall)]
76 public extern static string GetCurrentDirectory ();
78 [MethodImplAttribute (MethodImplOptions.InternalCall)]
79 public extern static bool SetCurrentDirectory (string path);
83 [MethodImplAttribute (MethodImplOptions.InternalCall)]
84 public extern static bool MoveFile (string path, string dest);
86 [MethodImplAttribute (MethodImplOptions.InternalCall)]
87 public extern static bool CopyFile (string path, string dest, bool overwrite);
89 [MethodImplAttribute (MethodImplOptions.InternalCall)]
90 public extern static bool DeleteFile (string path);
92 [MethodImplAttribute (MethodImplOptions.InternalCall)]
93 public extern static FileAttributes GetFileAttributes (string path);
95 [MethodImplAttribute (MethodImplOptions.InternalCall)]
96 public extern static bool SetFileAttributes (string path, FileAttributes attrs);
98 public static bool Exists (string path)
100 FileAttributes attrs = GetFileAttributes (path);
101 if (attrs == InvalidFileAttributes)
107 public static bool ExistsFile (string path)
109 FileAttributes attrs = GetFileAttributes (path);
110 if (attrs == InvalidFileAttributes)
113 if ((attrs & FileAttributes.Directory) != 0)
119 public static bool ExistsDirectory (string path)
121 FileAttributes attrs = GetFileAttributes (path);
122 if (attrs == InvalidFileAttributes)
125 if ((attrs & FileAttributes.Directory) == 0)
131 [MethodImplAttribute (MethodImplOptions.InternalCall)]
132 public extern static bool GetFileStat (string path, out MonoIOStat stat);
136 [MethodImplAttribute (MethodImplOptions.InternalCall)]
137 public extern static IntPtr Open (string filename, FileMode mode, FileAccess access, FileShare share);
139 [MethodImplAttribute (MethodImplOptions.InternalCall)]
140 public extern static bool Close (IntPtr handle);
142 [MethodImplAttribute (MethodImplOptions.InternalCall)]
143 public extern static int Read (IntPtr handle, byte [] dest, int dest_offset, int count);
145 [MethodImplAttribute (MethodImplOptions.InternalCall)]
146 public extern static int Write (IntPtr handle, byte [] src, int src_offset, int count);
148 [MethodImplAttribute (MethodImplOptions.InternalCall)]
149 public extern static long Seek (IntPtr handle, long offset, SeekOrigin origin);
151 [MethodImplAttribute (MethodImplOptions.InternalCall)]
152 public extern static bool Flush (IntPtr handle);
154 [MethodImplAttribute (MethodImplOptions.InternalCall)]
155 public extern static long GetLength (IntPtr handle);
157 [MethodImplAttribute (MethodImplOptions.InternalCall)]
158 public extern static bool SetLength (IntPtr handle, long length);
160 [MethodImplAttribute (MethodImplOptions.InternalCall)]
161 public extern static bool SetFileTime (IntPtr handle, long creation_time, long last_access_time, long last_write_time);
163 public static bool SetFileTime (string path, long creation_time, long last_access_time, long last_write_time)
168 handle = Open (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
169 if (handle == IntPtr.Zero)
172 result = SetFileTime (handle, creation_time, last_access_time, last_write_time);
180 public extern static IntPtr ConsoleOutput {
181 [MethodImplAttribute (MethodImplOptions.InternalCall)]
185 public extern static IntPtr ConsoleInput {
186 [MethodImplAttribute (MethodImplOptions.InternalCall)]
190 public extern static IntPtr ConsoleError {
191 [MethodImplAttribute (MethodImplOptions.InternalCall)]
197 [MethodImplAttribute (MethodImplOptions.InternalCall)]
198 public extern static bool CreatePipe (out IntPtr read_handle, out IntPtr write_handle);
202 public extern static char VolumeSeparatorChar {
203 [MethodImplAttribute (MethodImplOptions.InternalCall)]
207 public extern static char DirectorySeparatorChar {
208 [MethodImplAttribute (MethodImplOptions.InternalCall)]
212 public extern static char AltDirectorySeparatorChar {
213 [MethodImplAttribute (MethodImplOptions.InternalCall)]
217 public extern static char PathSeparator {
218 [MethodImplAttribute (MethodImplOptions.InternalCall)]
222 public extern static char [] InvalidPathChars {
223 [MethodImplAttribute (MethodImplOptions.InternalCall)]