mscorlib(4.0.0.0) API with additions
__Error.cs
1 using Microsoft.Win32;
3 using System.Security;
5 
6 namespace System.IO
7 {
8  internal static class __Error
9  {
10  internal const int ERROR_FILE_NOT_FOUND = 2;
11 
12  internal const int ERROR_PATH_NOT_FOUND = 3;
13 
14  internal const int ERROR_ACCESS_DENIED = 5;
15 
16  internal const int ERROR_INVALID_PARAMETER = 87;
17 
18  internal static void EndOfFile()
19  {
20  throw new EndOfStreamException(Environment.GetResourceString("IO.EOF_ReadBeyondEOF"));
21  }
22 
23  internal static void FileNotOpen()
24  {
25  throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_FileClosed"));
26  }
27 
28  internal static void StreamIsClosed()
29  {
30  throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_StreamClosed"));
31  }
32 
33  internal static void MemoryStreamNotExpandable()
34  {
35  throw new NotSupportedException(Environment.GetResourceString("NotSupported_MemStreamNotExpandable"));
36  }
37 
38  internal static void ReaderClosed()
39  {
40  throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ReaderClosed"));
41  }
42 
43  internal static void ReadNotSupported()
44  {
45  throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnreadableStream"));
46  }
47 
48  internal static void SeekNotSupported()
49  {
50  throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnseekableStream"));
51  }
52 
53  internal static void WrongAsyncResult()
54  {
55  throw new ArgumentException(Environment.GetResourceString("Arg_WrongAsyncResult"));
56  }
57 
58  internal static void EndReadCalledTwice()
59  {
60  throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndReadCalledMultiple"));
61  }
62 
63  internal static void EndWriteCalledTwice()
64  {
65  throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndWriteCalledMultiple"));
66  }
67 
68  [SecurityCritical]
69  internal static string GetDisplayablePath(string path, bool isInvalidPath)
70  {
71  if (string.IsNullOrEmpty(path))
72  {
73  return string.Empty;
74  }
75  if (path.Length < 2)
76  {
77  return path;
78  }
79  if (PathInternal.IsPartiallyQualified(path) && !isInvalidPath)
80  {
81  return path;
82  }
83  bool flag = false;
84  try
85  {
86  if (!isInvalidPath)
87  {
88  FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, path, checkForDuplicates: false, needFullPath: false);
89  flag = true;
90  }
91  }
92  catch (SecurityException)
93  {
94  }
95  catch (ArgumentException)
96  {
97  }
98  catch (NotSupportedException)
99  {
100  }
101  if (!flag)
102  {
103  path = ((!Path.IsDirectorySeparator(path[path.Length - 1])) ? Path.GetFileName(path) : Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName"));
104  }
105  return path;
106  }
107 
108  [SecuritySafeCritical]
109  internal static void WinIOError()
110  {
111  int lastWin32Error = Marshal.GetLastWin32Error();
112  WinIOError(lastWin32Error, string.Empty);
113  }
114 
115  [SecurityCritical]
116  internal static void WinIOError(int errorCode, string maybeFullPath)
117  {
118  bool isInvalidPath = errorCode == 123 || errorCode == 161;
119  string displayablePath = GetDisplayablePath(maybeFullPath, isInvalidPath);
120  switch (errorCode)
121  {
122  case 2:
123  if (displayablePath.Length == 0)
124  {
125  throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound"));
126  }
127  throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound_FileName", displayablePath), displayablePath);
128  case 3:
129  if (displayablePath.Length == 0)
130  {
131  throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_NoPathName"));
132  }
133  throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_Path", displayablePath));
134  case 5:
135  if (displayablePath.Length == 0)
136  {
137  throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_NoPathName"));
138  }
139  throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", displayablePath));
140  case 183:
141  if (displayablePath.Length != 0)
142  {
143  throw new IOException(Environment.GetResourceString("IO.IO_AlreadyExists_Name", displayablePath), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
144  }
145  break;
146  case 206:
147  throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
148  case 15:
149  throw new DriveNotFoundException(Environment.GetResourceString("IO.DriveNotFound_Drive", displayablePath));
150  case 87:
151  throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
152  case 32:
153  if (displayablePath.Length == 0)
154  {
155  throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_NoFileName"), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
156  }
157  throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_File", displayablePath), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
158  case 80:
159  if (displayablePath.Length != 0)
160  {
161  throw new IOException(Environment.GetResourceString("IO.IO_FileExists_Name", displayablePath), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
162  }
163  break;
164  case 995:
165  throw new OperationCanceledException();
166  }
167  throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
168  }
169 
170  [SecuritySafeCritical]
171  internal static void WinIODriveError(string driveName)
172  {
173  int lastWin32Error = Marshal.GetLastWin32Error();
174  WinIODriveError(driveName, lastWin32Error);
175  }
176 
177  [SecurityCritical]
178  internal static void WinIODriveError(string driveName, int errorCode)
179  {
180  if (errorCode == 3 || errorCode == 15)
181  {
182  throw new DriveNotFoundException(Environment.GetResourceString("IO.DriveNotFound_Drive", driveName));
183  }
184  WinIOError(errorCode, driveName);
185  }
186 
187  internal static void WriteNotSupported()
188  {
189  throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnwritableStream"));
190  }
191 
192  internal static void WriterClosed()
193  {
194  throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_WriterClosed"));
195  }
196  }
197 }
FileIOPermissionAccess
Specifies the type of file access requested.
The end of the file has been reached successfully.
Definition: __Canon.cs:3
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Controls the ability to access files and folders. This class cannot be inherited.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
The P:System.Uri.LocalPath data.
The exception that is thrown when a security error is detected.