Esempio n. 1
0
        private void VerifyInstanceType(NativeMethods.WIN32_FIND_DATA win32FindData)
        {
            var regularPath = Path.GetCleanExceptionPath(InputPath);

             var isFolder = (win32FindData.dwFileAttributes & FileAttributes.Directory) != 0;

             if (IsDirectory)
             {
            if (!isFolder)
               throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "({0}) {1}", Win32Errors.ERROR_PATH_NOT_FOUND, string.Format(CultureInfo.InvariantCulture, Resources.Target_Directory_Is_A_File, regularPath)));
             }

             else if (isFolder)
            throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "({0}) {1}", Win32Errors.ERROR_FILE_NOT_FOUND, string.Format(CultureInfo.InvariantCulture, Resources.Target_File_Is_A_Directory, regularPath)));
        }
Esempio n. 2
0
        /// <summary>[AlphaFS] Checks if specified <paramref name="path"/> is a local- or network drive.</summary>
        /// <returns><c>true</c> if the  drive exists, <c>false</c> otherwise.</returns>
        internal static bool ExistsDriveOrFolderOrFile(KernelTransaction transaction, string path, bool isFolder, int lastError, bool throwIfDriveNotExists, bool throwIfFolderOrFileNotExists)
        {
            if (Utils.IsNullOrWhiteSpace(path))
            {
                return(false);
            }


            var drive = GetDirectoryRootCore(transaction, path, PathFormat.FullPath);

            var driveExists = null != drive && File.ExistsCore(transaction, true, drive, PathFormat.FullPath);

            var regularPath = Path.GetCleanExceptionPath(path);


            if (!driveExists && throwIfDriveNotExists || lastError == Win32Errors.ERROR_NOT_READY)
            {
                throw new DeviceNotReadyException(drive, true);
            }


            throwIfFolderOrFileNotExists = throwIfFolderOrFileNotExists && lastError != Win32Errors.NO_ERROR;

            if (throwIfFolderOrFileNotExists)
            {
                if (lastError != Win32Errors.NO_ERROR)
                {
                    if (lastError == Win32Errors.ERROR_PATH_NOT_FOUND)
                    {
                        throw new DirectoryNotFoundException(regularPath);
                    }


                    if (lastError == Win32Errors.ERROR_FILE_NOT_FOUND)
                    {
                        if (isFolder)
                        {
                            throw new DirectoryNotFoundException(regularPath);
                        }

                        throw new FileNotFoundException(regularPath);
                    }
                }
            }


            return(driveExists);
        }
        private void ThrowPossibleException(uint lastError, string pathLp)
        {
            switch (lastError)
            {
            case Win32Errors.ERROR_NO_MORE_FILES:
                lastError = Win32Errors.NO_ERROR;
                break;


            case Win32Errors.ERROR_FILE_NOT_FOUND: // On files.
            case Win32Errors.ERROR_PATH_NOT_FOUND: // On folders.
            case Win32Errors.ERROR_NOT_READY:      // DeviceNotReadyException: Floppy device or network drive not ready.
                // MSDN: .NET 3.5+: DirectoryNotFoundException: Path is invalid, such as referring to an unmapped drive.
                // Directory.Delete()

                lastError = IsDirectory ? (int)Win32Errors.ERROR_PATH_NOT_FOUND : Win32Errors.ERROR_FILE_NOT_FOUND;
                break;


                //case Win32Errors.ERROR_DIRECTORY:
                //   // MSDN: .NET 3.5+: IOException: path is a file name.
                //   // Directory.EnumerateDirectories()
                //   // Directory.EnumerateFiles()
                //   // Directory.EnumerateFileSystemEntries()
                //   // Directory.GetDirectories()
                //   // Directory.GetFiles()
                //   // Directory.GetFileSystemEntries()
                //   break;

                //case Win32Errors.ERROR_ACCESS_DENIED:
                //   // MSDN: .NET 3.5+: UnauthorizedAccessException: The caller does not have the required permission.
                //   break;
            }


            if (lastError != Win32Errors.NO_ERROR)
            {
                var regularPath = Path.GetCleanExceptionPath(pathLp);

                // Pass control to the ErrorHandler when set.
                if (null == ErrorHandler || !ErrorHandler((int)lastError, new Win32Exception((int)lastError).Message, regularPath))
                {
                    // When the ErrorHandler returns false, thrown the Exception.
                    NativeError.ThrowException(lastError, regularPath);
                }
            }
        }
 /// <summary>[AlphaFS] Initializes a new instance of the <see cref="DeviceNotReadyException"/> class.</summary>
 /// <param name="path">The path to the device.</param>
 /// <param name="innerException">The inner exception.</param>
 public DeviceNotReadyException(string path, Exception innerException) : base(string.Format(CultureInfo.InvariantCulture, "{0}: [{1}]", ErrorText, Path.GetCleanExceptionPath(path)), innerException)
 {
 }
Esempio n. 5
0
 /// <summary>[AlphaFS] Initializes a new instance of the <see cref="FileReadOnlyException"/> class.</summary>
 /// <param name="path">The path to the file.</param>
 public FileReadOnlyException(string path) : base(string.Format(CultureInfo.InvariantCulture, "{0}: [{1}]", ErrorText, Path.GetCleanExceptionPath(path)))
 {
 }