2 // System.IO.MonoIO.cs: static interface to native filesystem.
\r
5 // Dan Lewis (dihlewis@yahoo.co.uk)
\r
11 using System.Runtime.CompilerServices;
\r
15 internal sealed class MonoIO {
\r
16 public static readonly FileAttributes
\r
17 InvalidFileAttributes = (FileAttributes)(-1);
\r
19 public static readonly IntPtr
\r
20 InvalidHandle = (IntPtr)(-1);
\r
24 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
25 public extern static MonoIOError GetLastError ();
\r
27 public static Exception GetException ()
\r
29 return GetException (String.Empty);
\r
32 public static Exception GetException (string path)
\r
34 MonoIOError error = GetLastError ();
\r
38 // FIXME: add more exception mappings here
\r
39 case MonoIOError.ERROR_FILE_NOT_FOUND:
\r
40 message = String.Format ("Could not find file \"{0}\"", path);
\r
41 return new FileNotFoundException (message);
\r
43 case MonoIOError.ERROR_PATH_NOT_FOUND:
\r
44 message = String.Format ("Could not find a part of the path \"{0}\"", path);
\r
45 return new DirectoryNotFoundException (message);
\r
47 case MonoIOError.ERROR_ACCESS_DENIED:
\r
48 message = String.Format ("Access to the path \"{0}\" is denied.", path);
\r
49 return new UnauthorizedAccessException (message);
\r
52 string name = Enum.GetName (typeof (MonoIOError), error);
\r
53 message = String.Format ("Win32 IO operation returned {0} for \"{1}\"", name, path);
\r
54 return new IOException (message);
\r
58 // directory methods
\r
60 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
61 public extern static bool CreateDirectory (string path);
\r
63 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
64 public extern static bool RemoveDirectory (string path);
\r
66 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
67 public extern static IntPtr FindFirstFile (string path, out MonoIOStat stat);
\r
69 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
70 public extern static bool FindNextFile (IntPtr find, out MonoIOStat stat);
\r
72 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
73 public extern static bool FindClose (IntPtr find);
\r
75 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
76 public extern static string GetCurrentDirectory ();
\r
78 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
79 public extern static bool SetCurrentDirectory (string path);
\r
83 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
84 public extern static bool MoveFile (string path, string dest);
\r
86 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
87 public extern static bool CopyFile (string path, string dest, bool overwrite);
\r
89 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
90 public extern static bool DeleteFile (string path);
\r
92 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
93 public extern static FileAttributes GetFileAttributes (string path);
\r
95 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
96 public extern static bool SetFileAttributes (string path, FileAttributes attrs);
\r
98 public static bool Exists (string path)
\r
100 FileAttributes attrs = GetFileAttributes (path);
\r
101 if (attrs == InvalidFileAttributes)
\r
107 public static bool ExistsFile (string path)
\r
109 FileAttributes attrs = GetFileAttributes (path);
\r
110 if (attrs == InvalidFileAttributes)
\r
113 if ((attrs & FileAttributes.Directory) != 0)
\r
119 public static bool ExistsDirectory (string path)
\r
121 FileAttributes attrs = GetFileAttributes (path);
\r
122 if (attrs == InvalidFileAttributes)
\r
125 if ((attrs & FileAttributes.Directory) == 0)
\r
131 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
132 public extern static bool GetFileStat (string path, out MonoIOStat stat);
\r
136 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
137 public extern static IntPtr Open (string filename, FileMode mode, FileAccess access, FileShare share);
\r
139 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
140 public extern static bool Close (IntPtr handle);
\r
142 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
143 public extern static int Read (IntPtr handle, byte [] dest, int dest_offset, int count);
\r
145 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
146 public extern static int Write (IntPtr handle, byte [] src, int src_offset, int count);
\r
148 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
149 public extern static long Seek (IntPtr handle, long offset, SeekOrigin origin);
\r
151 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
152 public extern static bool Flush (IntPtr handle);
\r
154 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
155 public extern static long GetLength (IntPtr handle);
\r
157 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
158 public extern static bool SetLength (IntPtr handle, long length);
\r
160 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
161 public extern static bool SetFileTime (IntPtr handle, long creation_time, long last_access_time, long last_write_time);
\r
163 public static bool SetFileTime (string path, long creation_time, long last_access_time, long last_write_time)
\r
168 handle = Open (path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
\r
169 if (handle == IntPtr.Zero)
\r
172 result = SetFileTime (handle, creation_time, last_access_time, last_write_time);
\r
180 public extern static IntPtr ConsoleOutput {
\r
181 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
185 public extern static IntPtr ConsoleInput {
\r
186 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
190 public extern static IntPtr ConsoleError {
\r
191 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
197 public extern static char VolumeSeparatorChar {
\r
198 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
202 public extern static char DirectorySeparatorChar {
\r
203 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
207 public extern static char AltDirectorySeparatorChar {
\r
208 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
212 public extern static char PathSeparator {
\r
213 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r
217 public extern static char [] InvalidPathChars {
\r
218 [MethodImplAttribute (MethodImplOptions.InternalCall)]
\r