Ejemplo n.º 1
0
        /// <summary>
        /// Exception Mapping
        /// </summary>
        /// <param name="path"></param>
        /// <param name="errorCode">errorCode</param>
        public static void NativeExceptionMapping(String path, Int32 errorCode)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(path));

            if (errorCode == Win32ErrorCodes.ERROR_SUCCESS)
            {
                return;
            }

            string affectedPath = path;

            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

            switch (errorCode)
            {
            case Win32ErrorCodes.ERROR_PATH_NOT_FOUND:
            case Win32ErrorCodes.ERROR_FILE_NOT_FOUND:
                throw new PathNotFoundException(Win32ErrorCodes.FormatMessage(errorCode), affectedPath);

            case Win32ErrorCodes.ERROR_ALREADY_EXISTS:
                throw new PathAlreadyExistsException(Win32ErrorCodes.FormatMessage(errorCode), affectedPath);

            case Win32ErrorCodes.ERROR_INVALID_NAME:
            case Win32ErrorCodes.ERROR_DIRECTORY:
                throw new InvalidPathException(Win32ErrorCodes.FormatMessage(errorCode), affectedPath);

            case Win32ErrorCodes.ERROR_REM_NOT_LIST:
            case Win32ErrorCodes.ERROR_NETWORK_BUSY:
            case Win32ErrorCodes.ERROR_BUSY:
            case Win32ErrorCodes.ERROR_PATH_BUSY:
                throw new FileSystemIsBusyException(Win32ErrorCodes.FormatMessage(errorCode), affectedPath);

            case Win32ErrorCodes.ERROR_DIR_NOT_EMPTY:
                throw new DirectoryNotEmptyException(Win32ErrorCodes.FormatMessage(errorCode), affectedPath);

            case Win32ErrorCodes.ERROR_ACCESS_DENIED:
            case Win32ErrorCodes.ERROR_NETWORK_ACCESS_DENIED:
                throw new UnauthorizedAccessException("Access to '" + affectedPath + "' denied.", new Win32Exception(errorCode));

            case Win32ErrorCodes.ERROR_CURRENT_DIRECTORY:
            case Win32ErrorCodes.ERROR_CANNOT_MAKE:
                throw new Exception(Win32ErrorCodes.FormatMessage(errorCode) + affectedPath + "'.", new Win32Exception(errorCode));

            default:
                throw new Exception("Error on '" + affectedPath + "': See InnerException for details.", new Win32Exception(errorCode));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Moves to next element
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            do
            {
                // only at start currentFileHandle is null
                if (_currentFileHandle == null)
                {
                    // first call
                    _currentFileHandle = Win32SafeNativeMethods.FindFirstFile(DirectoryPath, _currentFindData);
                    _currentErrorCode  = Marshal.GetLastWin32Error();
                }
                else
                {
                    // second to n call
                    _currentFindData = new Win32FindData();
                    if (!Win32SafeNativeMethods.FindNextFile(_currentFileHandle, _currentFindData))
                    {
                        return(false);
                    }
                }

                // Take care of invalid handles
                if (_currentFileHandle.IsInvalid)
                {
                    if (_currentErrorCode != Win32ErrorCodes.ERROR_NO_MORE_FILES)
                    {
                        Win32ErrorCodes.NativeExceptionMapping(DirectoryPath, _currentErrorCode);
                    }

                    return(false);
                }

                // skip entries to ignore
            } while(FilterSystemEntries && _currentFindData.IsSystemDirectoryEntry());

            return(true);
        }