Пример #1
0
        internal static Exception GetIoExceptionForError(int error, string path = null)
        {
            // http://referencesource.microsoft.com/#mscorlib/system/io/__error.cs,142

            string errorText = $"{NativeErrorHelper.LastErrorToString(error)} : '{path ?? XTaskStrings.NoValue}'";

            switch (error)
            {
            case WinError.ERROR_FILE_NOT_FOUND:
                return(new FileNotFoundException(errorText, path));

            case WinError.ERROR_PATH_NOT_FOUND:
                return(new DirectoryNotFoundException(errorText));

            case WinError.ERROR_ACCESS_DENIED:
            // Network access doesn't throw UnauthorizedAccess in .NET
            case WinError.ERROR_NETWORK_ACCESS_DENIED:
                return(new UnauthorizedAccessException(errorText));

            case WinError.ERROR_FILENAME_EXCED_RANGE:
                return(new PathTooLongException(errorText));

            case WinError.ERROR_INVALID_DRIVE:
                return(new DriveNotFoundException(errorText));

            case WinError.ERROR_OPERATION_ABORTED:
                return(new OperationCanceledException(errorText));

            case WinError.ERROR_ALREADY_EXISTS:
            case WinError.ERROR_SHARING_VIOLATION:
            case WinError.ERROR_FILE_EXISTS:
            default:
                return(new IOException(errorText, NativeErrorHelper.GetHResultForWindowsError(error)));
            }
        }
Пример #2
0
            /// <summary>
            /// Set the attributes on the given path if possible.
            /// </summary>
            /// <remarks>
            /// This is far greater perf than System.IO as it can skip all of the validation and normalization.
            /// </remarks>
            /// <exception cref="System.UnauthorizedAccessException">Thrown if the current user does not have rights to the specified path.</exception>
            internal static bool TrySetFileAttributes(string path, FileAttributes attributes)
            {
                if (!Private.SetFileAttributesW(path, (uint)attributes))
                {
                    int lastError = Marshal.GetLastWin32Error();
                    switch (lastError)
                    {
                    case WinError.ERROR_ACCESS_DENIED:
                    case WinError.ERROR_NETWORK_ACCESS_DENIED:
                        throw new UnauthorizedAccessException(String.Format(CultureInfo.InvariantCulture, "{0} : '{1}'", NativeErrorHelper.LastErrorToString(lastError), path));
                    }

                    return(false);
                }

                return(true);
            }
Пример #3
0
            private static FileAttributes TryGetFileAttributesPrivate(string path)
            {
                path = Paths.AddExtendedPrefix(path);

                FileAttributes result = Private.GetFileAttributesW(path);

                if (result == InvalidFileAttributes)
                {
                    int error = Marshal.GetLastWin32Error();
                    switch (error)
                    {
                    case WinError.ERROR_ACCESS_DENIED:
                    case WinError.ERROR_NETWORK_ACCESS_DENIED:
                        throw new UnauthorizedAccessException(String.Format(CultureInfo.InvariantCulture, "{0} : '{1}'", NativeErrorHelper.LastErrorToString(error), path));
                    }
                }

                return(result);
            }