source: branches/eraser6/CodeReview/Eraser.Util/Win32ErrorCodes.cs @ 1576

Revision 1576, 2.3 KB checked in by lowjoel, 5 years ago (diff)

Throw an IOException when we can't access a given file instead of a FileLoadException? since FileLoadExceptions? are for assemblies which don't load properly. Addresses #284: Eraser.Util rewrite

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Runtime.InteropServices;
26using System.IO;
27
28namespace Eraser.Util
29{
30    public static class Win32ErrorCode
31    {
32        /// <summary>
33        /// Converts a Win32 Error code to a HRESULT.
34        /// </summary>
35        /// <param name="errorCode">The error code to convert.</param>
36        /// <returns>A HRESULT value representing the error code.</returns>
37        private static int GetHRForWin32Error(int errorCode)
38        {
39            const uint FACILITY_WIN32 = 7;
40            return errorCode <= 0 ? errorCode :
41                (int)((((uint)errorCode) & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000);
42        }
43
44        /// <summary>
45        /// Gets a Exception for the given Win32 error code.
46        /// </summary>
47        /// <param name="errorCode">The error code.</param>
48        /// <returns>An exception object representing the error code.</returns>
49        internal static Exception GetExceptionForWin32Error(int errorCode)
50        {
51            switch (errorCode)
52            {
53                case NoError:           return null;
54                case SharingViolation:  return new IOException();
55
56                default:
57                    int HR = GetHRForWin32Error(errorCode);
58                    return Marshal.GetExceptionForHR(HR);
59            }
60        }
61
62        public const int NoError = Success;
63        public const int Success = 0;
64        public const int InvalidFunction = 1;
65        public const int FileNotFound = 2;
66        public const int PathNotFound = 3;
67        public const int AccessDenied = 5;
68        public const int NoMoreFiles = 18;
69        public const int NotReady = 21;
70        public const int SharingViolation = 32;
71        public const int InvalidParameter = 87;
72        public const int MoreData = 234;
73        public const int UnrecognizedVolume = 1005;
74        public const int NotAReparsePoint = 4390;
75    }
76}
Note: See TracBrowser for help on using the repository browser.