Пример #1
0
        /**
         * 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 void Win32Error(int errorCode, String str)
        {
            switch (errorCode)
            {
            case Win32Native.ERROR_ACCESS_DENIED:
                if (str != null)
                {
                    throw new UnauthorizedAccessException(Environment.GetResourceString("UnauthorizedAccess_RegistryKeyGeneric_Key", str));
                }
                else
                {
                    throw new UnauthorizedAccessException();
                }

            case Win32Native.ERROR_INVALID_HANDLE:
                /**
                 * For normal RegistryKey instances we dispose the SafeRegHandle and throw IOException.
                 * However, for HKEY_PERFORMANCE_DATA (on a local or remote machine) we avoid disposing the
                 * SafeRegHandle and only throw the IOException.  This is to workaround reentrancy issues
                 * in PerformanceCounter.NextValue() where the API could throw {NullReference, ObjectDisposed, ArgumentNull}Exception
                 * on reentrant calls because of this error code path in RegistryKey
                 *
                 * Normally we'd make our caller synchronize access to a shared RegistryKey instead of doing something like this,
                 * however we shipped PerformanceCounter.NextValue() un-synchronized in v2.0RTM and customers have taken a dependency on
                 * this behavior (being able to simultaneously query multiple remote-machine counters on multiple threads, instead of
                 * having serialized access).
                 */
                if (!IsPerfDataKey())
                {
                    hkey.SetHandleAsInvalid();
                    hkey = null;
                }
                goto default;

            case Win32Native.ERROR_FILE_NOT_FOUND:
                throw new IOException(Environment.GetResourceString("Arg_RegKeyNotFound"), errorCode);

            default:
                throw new IOException(Win32Native.GetMessage(errorCode), errorCode);
            }
        }
Пример #2
0
        /**
         * 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 void Win32Error(int errorCode, string str)
        {
            switch (errorCode)
            {
            case Win32Native.ERROR_ACCESS_DENIED:
                if (str != null)
                {
                    throw new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccess_RegistryKeyGeneric_Key, str));
                }
                else
                {
                    throw new UnauthorizedAccessException();
                }

            case Win32Native.ERROR_FILE_NOT_FOUND:
                throw new IOException(SR.Arg_RegKeyNotFound, errorCode);

            default:
                throw new IOException(Win32Native.GetMessage(errorCode), errorCode);
            }
        }