Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / IO / __Error.cs / 1 / __Error.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: __Error
**
**
** Purpose: Centralized error methods for the IO package.
** Mostly useful for translating Win32 HRESULTs into meaningful
** error strings & exceptions.
**
**
===========================================================*/
using System;
using System.Runtime.InteropServices;
using Win32Native = Microsoft.Win32.Win32Native;
using System.Text;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
namespace System.IO {
// Only static data no need to serialize
internal static class __Error
{
internal static void EndOfFile() {
throw new EndOfStreamException(Environment.GetResourceString("IO.EOF_ReadBeyondEOF"));
}
internal static void FileNotOpen() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_FileClosed"));
}
internal static void StreamIsClosed() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_StreamClosed"));
}
internal static void MemoryStreamNotExpandable() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_MemStreamNotExpandable"));
}
internal static void ReaderClosed() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ReaderClosed"));
}
internal static void ReadNotSupported() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnreadableStream"));
}
internal static void SeekNotSupported() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnseekableStream"));
}
internal static void WrongAsyncResult() {
throw new ArgumentException(Environment.GetResourceString("Arg_WrongAsyncResult"));
}
internal static void EndReadCalledTwice() {
// Should ideally be InvalidOperationExc but we can't maitain parity with Stream and FileStream without some work
throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndReadCalledMultiple"));
}
internal static void EndWriteCalledTwice() {
// Should ideally be InvalidOperationExc but we can't maintain parity with Stream and FileStream without some work
throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndWriteCalledMultiple"));
}
// Given a possible fully qualified path, ensure that we have path
// discovery permission to that path. If we do not, return just the
// file name. If we know it is a directory, then don't return the
// directory name.
internal static String GetDisplayablePath(String path, bool isInvalidPath)
{
if (String.IsNullOrEmpty(path))
return path;
// Is it a fully qualified path?
bool isFullyQualified = false;
if (path.Length < 2)
return path;
if (Path.IsDirectorySeparator(path[0]) && Path.IsDirectorySeparator(path[1]))
isFullyQualified = true;
else if (path[1] == Path.VolumeSeparatorChar) {
isFullyQualified = true;
}
if (!isFullyQualified && !isInvalidPath)
return path;
bool safeToReturn = false;
try {
if (!isInvalidPath) {
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new String[] { path }, false, false).Demand();
safeToReturn = true;
}
}
catch(ArgumentException) {
// Needed for Win9x
}
catch(NotSupportedException) {
// Needed for Win9x, which will sometimes return
// ERROR_PATH_NOT_FOUND for invalid path names, but also
// will leak the current working directory.
}
catch(SecurityException) {
}
if (!safeToReturn) {
if (Path.IsDirectorySeparator(path[path.Length - 1]))
path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
else
path = Path.GetFileName(path);
}
return path;
}
internal static void WinIOError() {
int errorCode = Marshal.GetLastWin32Error();
WinIOError(errorCode, String.Empty);
}
// After calling GetLastWin32Error(), it clears the last error field,
// so you must save the HResult and pass it to this method. This method
// will determine the appropriate exception to throw dependent on your
// error, and depending on the error, insert a string into the message
// gotten from the ResourceManager.
internal static void WinIOError(int errorCode, String maybeFullPath) {
// This doesn't have to be perfect, but is a perf optimization.
bool isInvalidPath = errorCode == Win32Native.ERROR_INVALID_NAME || errorCode == Win32Native.ERROR_BAD_PATHNAME;
String str = GetDisplayablePath(maybeFullPath, isInvalidPath);
switch (errorCode) {
case Win32Native.ERROR_FILE_NOT_FOUND:
if (str.Length == 0)
throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound"));
else
throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.FileNotFound_FileName"), str), str);
case Win32Native.ERROR_PATH_NOT_FOUND:
if (str.Length == 0)
throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_NoPathName"));
else
throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.PathNotFound_Path"), str));
case Win32Native.ERROR_ACCESS_DENIED:
if (str.Length == 0)
throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_NoPathName"));
else
throw new UnauthorizedAccessException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), str));
case Win32Native.ERROR_ALREADY_EXISTS:
if (str.Length == 0)
goto default;
throw new IOException(Environment.GetResourceString("IO.IO_AlreadyExists_Name", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_FILENAME_EXCED_RANGE:
throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
case Win32Native.ERROR_INVALID_DRIVE:
throw new DriveNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.DriveNotFound_Drive"), str));
case Win32Native.ERROR_INVALID_PARAMETER:
throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_SHARING_VIOLATION:
if (str.Length == 0)
throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_NoFileName"), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
else
throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_File", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_FILE_EXISTS:
if (str.Length == 0)
goto default;
throw new IOException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.IO_FileExists_Name"), str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_OPERATION_ABORTED:
throw new OperationCanceledException();
default:
throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
}
}
// An alternative to WinIOError with friendlier messages for drives
internal static void WinIODriveError(String driveName) {
int errorCode = Marshal.GetLastWin32Error();
WinIODriveError(driveName, errorCode);
}
internal static void WinIODriveError(String driveName, int errorCode)
{
switch (errorCode) {
case Win32Native.ERROR_PATH_NOT_FOUND:
case Win32Native.ERROR_INVALID_DRIVE:
throw new DriveNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.DriveNotFound_Drive"), driveName));
default:
WinIOError(errorCode, driveName);
break;
}
}
internal static void WriteNotSupported() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnwritableStream"));
}
internal static void WriterClosed() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_WriterClosed"));
}
// From WinError.h
internal const int ERROR_FILE_NOT_FOUND = Win32Native.ERROR_FILE_NOT_FOUND;
internal const int ERROR_PATH_NOT_FOUND = Win32Native.ERROR_PATH_NOT_FOUND;
internal const int ERROR_ACCESS_DENIED = Win32Native.ERROR_ACCESS_DENIED;
internal const int ERROR_INVALID_PARAMETER = Win32Native.ERROR_INVALID_PARAMETER;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: __Error
**
**
** Purpose: Centralized error methods for the IO package.
** Mostly useful for translating Win32 HRESULTs into meaningful
** error strings & exceptions.
**
**
===========================================================*/
using System;
using System.Runtime.InteropServices;
using Win32Native = Microsoft.Win32.Win32Native;
using System.Text;
using System.Globalization;
using System.Security;
using System.Security.Permissions;
namespace System.IO {
// Only static data no need to serialize
internal static class __Error
{
internal static void EndOfFile() {
throw new EndOfStreamException(Environment.GetResourceString("IO.EOF_ReadBeyondEOF"));
}
internal static void FileNotOpen() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_FileClosed"));
}
internal static void StreamIsClosed() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_StreamClosed"));
}
internal static void MemoryStreamNotExpandable() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_MemStreamNotExpandable"));
}
internal static void ReaderClosed() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_ReaderClosed"));
}
internal static void ReadNotSupported() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnreadableStream"));
}
internal static void SeekNotSupported() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnseekableStream"));
}
internal static void WrongAsyncResult() {
throw new ArgumentException(Environment.GetResourceString("Arg_WrongAsyncResult"));
}
internal static void EndReadCalledTwice() {
// Should ideally be InvalidOperationExc but we can't maitain parity with Stream and FileStream without some work
throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndReadCalledMultiple"));
}
internal static void EndWriteCalledTwice() {
// Should ideally be InvalidOperationExc but we can't maintain parity with Stream and FileStream without some work
throw new ArgumentException(Environment.GetResourceString("InvalidOperation_EndWriteCalledMultiple"));
}
// Given a possible fully qualified path, ensure that we have path
// discovery permission to that path. If we do not, return just the
// file name. If we know it is a directory, then don't return the
// directory name.
internal static String GetDisplayablePath(String path, bool isInvalidPath)
{
if (String.IsNullOrEmpty(path))
return path;
// Is it a fully qualified path?
bool isFullyQualified = false;
if (path.Length < 2)
return path;
if (Path.IsDirectorySeparator(path[0]) && Path.IsDirectorySeparator(path[1]))
isFullyQualified = true;
else if (path[1] == Path.VolumeSeparatorChar) {
isFullyQualified = true;
}
if (!isFullyQualified && !isInvalidPath)
return path;
bool safeToReturn = false;
try {
if (!isInvalidPath) {
new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new String[] { path }, false, false).Demand();
safeToReturn = true;
}
}
catch(ArgumentException) {
// Needed for Win9x
}
catch(NotSupportedException) {
// Needed for Win9x, which will sometimes return
// ERROR_PATH_NOT_FOUND for invalid path names, but also
// will leak the current working directory.
}
catch(SecurityException) {
}
if (!safeToReturn) {
if (Path.IsDirectorySeparator(path[path.Length - 1]))
path = Environment.GetResourceString("IO.IO_NoPermissionToDirectoryName");
else
path = Path.GetFileName(path);
}
return path;
}
internal static void WinIOError() {
int errorCode = Marshal.GetLastWin32Error();
WinIOError(errorCode, String.Empty);
}
// After calling GetLastWin32Error(), it clears the last error field,
// so you must save the HResult and pass it to this method. This method
// will determine the appropriate exception to throw dependent on your
// error, and depending on the error, insert a string into the message
// gotten from the ResourceManager.
internal static void WinIOError(int errorCode, String maybeFullPath) {
// This doesn't have to be perfect, but is a perf optimization.
bool isInvalidPath = errorCode == Win32Native.ERROR_INVALID_NAME || errorCode == Win32Native.ERROR_BAD_PATHNAME;
String str = GetDisplayablePath(maybeFullPath, isInvalidPath);
switch (errorCode) {
case Win32Native.ERROR_FILE_NOT_FOUND:
if (str.Length == 0)
throw new FileNotFoundException(Environment.GetResourceString("IO.FileNotFound"));
else
throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.FileNotFound_FileName"), str), str);
case Win32Native.ERROR_PATH_NOT_FOUND:
if (str.Length == 0)
throw new DirectoryNotFoundException(Environment.GetResourceString("IO.PathNotFound_NoPathName"));
else
throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.PathNotFound_Path"), str));
case Win32Native.ERROR_ACCESS_DENIED:
if (str.Length == 0)
throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_IODenied_NoPathName"));
else
throw new UnauthorizedAccessException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), str));
case Win32Native.ERROR_ALREADY_EXISTS:
if (str.Length == 0)
goto default;
throw new IOException(Environment.GetResourceString("IO.IO_AlreadyExists_Name", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_FILENAME_EXCED_RANGE:
throw new PathTooLongException(Environment.GetResourceString("IO.PathTooLong"));
case Win32Native.ERROR_INVALID_DRIVE:
throw new DriveNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.DriveNotFound_Drive"), str));
case Win32Native.ERROR_INVALID_PARAMETER:
throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_SHARING_VIOLATION:
if (str.Length == 0)
throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_NoFileName"), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
else
throw new IOException(Environment.GetResourceString("IO.IO_SharingViolation_File", str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_FILE_EXISTS:
if (str.Length == 0)
goto default;
throw new IOException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.IO_FileExists_Name"), str), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
case Win32Native.ERROR_OPERATION_ABORTED:
throw new OperationCanceledException();
default:
throw new IOException(Win32Native.GetMessage(errorCode), Win32Native.MakeHRFromErrorCode(errorCode), maybeFullPath);
}
}
// An alternative to WinIOError with friendlier messages for drives
internal static void WinIODriveError(String driveName) {
int errorCode = Marshal.GetLastWin32Error();
WinIODriveError(driveName, errorCode);
}
internal static void WinIODriveError(String driveName, int errorCode)
{
switch (errorCode) {
case Win32Native.ERROR_PATH_NOT_FOUND:
case Win32Native.ERROR_INVALID_DRIVE:
throw new DriveNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.DriveNotFound_Drive"), driveName));
default:
WinIOError(errorCode, driveName);
break;
}
}
internal static void WriteNotSupported() {
throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnwritableStream"));
}
internal static void WriterClosed() {
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_WriterClosed"));
}
// From WinError.h
internal const int ERROR_FILE_NOT_FOUND = Win32Native.ERROR_FILE_NOT_FOUND;
internal const int ERROR_PATH_NOT_FOUND = Win32Native.ERROR_PATH_NOT_FOUND;
internal const int ERROR_ACCESS_DENIED = Win32Native.ERROR_ACCESS_DENIED;
internal const int ERROR_INVALID_PARAMETER = Win32Native.ERROR_INVALID_PARAMETER;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XmlDataSource.cs
- TableLayout.cs
- ObjectReferenceStack.cs
- XPathAncestorQuery.cs
- Column.cs
- Condition.cs
- COM2ExtendedUITypeEditor.cs
- HotCommands.cs
- XmlNodeWriter.cs
- ConfigurationException.cs
- FunctionCommandText.cs
- Point3DValueSerializer.cs
- ArraySortHelper.cs
- cookiecontainer.cs
- OleDbCommand.cs
- ReachDocumentReferenceSerializer.cs
- DataGrid.cs
- TagPrefixCollection.cs
- MailWebEventProvider.cs
- MutexSecurity.cs
- COMException.cs
- UInt32Storage.cs
- SplitterPanel.cs
- ViewGenerator.cs
- ArgumentNullException.cs
- FixedTextSelectionProcessor.cs
- ParameterToken.cs
- ActivityMetadata.cs
- WindowVisualStateTracker.cs
- MetadataCache.cs
- ResetableIterator.cs
- BrowserCapabilitiesFactory35.cs
- BooleanAnimationBase.cs
- AssemblyResourceLoader.cs
- MatrixAnimationBase.cs
- SspiNegotiationTokenAuthenticator.cs
- CapiHashAlgorithm.cs
- DecoderBestFitFallback.cs
- AppAction.cs
- InternalConfigEventArgs.cs
- RangeBaseAutomationPeer.cs
- StreamGeometry.cs
- SendSecurityHeaderElementContainer.cs
- HashSetDebugView.cs
- COM2IVsPerPropertyBrowsingHandler.cs
- XmlAnyElementAttributes.cs
- FillRuleValidation.cs
- WebPartConnectionsCancelVerb.cs
- WinEventHandler.cs
- Globals.cs
- CompatibleComparer.cs
- SpnegoTokenProvider.cs
- AutomationPeer.cs
- PenContexts.cs
- __ComObject.cs
- QueryOutputWriterV1.cs
- ToolZone.cs
- xmlglyphRunInfo.cs
- ToolStripItemBehavior.cs
- DbDataAdapter.cs
- SubstitutionList.cs
- SqlGatherConsumedAliases.cs
- ServicesExceptionNotHandledEventArgs.cs
- BitmapEncoder.cs
- HTTPNotFoundHandler.cs
- RectangleConverter.cs
- RowUpdatedEventArgs.cs
- ExceptionValidationRule.cs
- XmlSerializerAssemblyAttribute.cs
- KernelTypeValidation.cs
- BaseDataBoundControlDesigner.cs
- sortedlist.cs
- ExpressionNode.cs
- ComponentChangingEvent.cs
- SecurityDescriptor.cs
- AppDomainUnloadedException.cs
- StyleHelper.cs
- RectValueSerializer.cs
- GroupBox.cs
- MarshalByValueComponent.cs
- ReaderContextStackData.cs
- remotingproxy.cs
- OracleInternalConnection.cs
- ReadOnlyTernaryTree.cs
- SubMenuStyleCollection.cs
- InternalConfigHost.cs
- xdrvalidator.cs
- WebHttpDispatchOperationSelector.cs
- GenericEnumerator.cs
- CharacterShapingProperties.cs
- OleTxTransactionInfo.cs
- OpenTypeCommon.cs
- Triangle.cs
- Blend.cs
- MappingException.cs
- CompressedStack.cs
- BitmapData.cs
- XamlToRtfParser.cs
- ExternalCalls.cs
- TraceAsyncResult.cs