Esempio n. 1
0
        private SafeFindFileHandle FindFirstFile(string pathLp, out NativeMethods.WIN32_FIND_DATA win32FindData, bool suppressException = false)
        {
            int lastError;
            var searchOption = null != FileSystemObjectType && (bool)FileSystemObjectType ? NativeMethods.FINDEX_SEARCH_OPS.SearchLimitToDirectories : NativeMethods.FINDEX_SEARCH_OPS.SearchNameMatch;

            var handle = FileSystemInfo.FindFirstFileNative(Transaction, pathLp, FindExInfoLevel, searchOption, LargeCache, out lastError, out win32FindData);


            if (!suppressException && !ContinueOnException)
            {
                if (null == handle)
                {
                    switch ((uint)lastError)
                    {
                    case Win32Errors.ERROR_FILE_NOT_FOUND: // FileNotFoundException.
                    case Win32Errors.ERROR_PATH_NOT_FOUND: // DirectoryNotFoundException.
                    case Win32Errors.ERROR_NOT_READY:      // DeviceNotReadyException: Floppy device or network drive not ready.

                        Directory.ExistsDriveOrFolderOrFile(Transaction, pathLp, IsDirectory, lastError, true, true);
                        break;
                    }


                    ThrowPossibleException((uint)lastError, pathLp);
                }

                //// When the handle is null and we are still here, it means the ErrorHandler is active, preventing the Exception from being thrown.

                //if (null != handle)
                //   VerifyInstanceType(win32FindData);
            }


            return(handle);
        }
        internal static int FillAttributeInfoCore(KernelTransaction transaction, string pathLp, ref NativeMethods.WIN32_FILE_ATTRIBUTE_DATA win32AttrData, bool tryAgain, bool returnErrorOnNotFound)
        {
            var lastError = (int)Win32Errors.ERROR_SUCCESS;

            #region Try Again

            // Someone has a handle to the file open, or other error.
            if (tryAgain)
            {
                NativeMethods.WIN32_FIND_DATA win32FindData;

                using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors))
                {
                    var handle = FileSystemInfo.FindFirstFileNative(transaction, pathLp, NativeMethods.FindexInfoLevel, NativeMethods.FINDEX_SEARCH_OPS.SearchNameMatch, NativeMethods.UseLargeCache, out lastError, out win32FindData);

                    if (null == handle)
                    {
                        switch ((uint)lastError)
                        {
                        case Win32Errors.ERROR_INVALID_NAME:
                        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.

                            if (!returnErrorOnNotFound)
                            {
                                // Return default value for backward compatibility.
                                lastError = (int)Win32Errors.ERROR_SUCCESS;

                                win32AttrData.dwFileAttributes = NativeMethods.InvalidFileAttributes;
                            }

                            break;
                        }

                        return(lastError);
                    }
                }

                // Copy the attribute information.
                win32AttrData = new NativeMethods.WIN32_FILE_ATTRIBUTE_DATA(win32FindData);
            }

            #endregion // Try Again

            else
            {
                using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors))
                {
                    if (!(null == transaction || !NativeMethods.IsAtLeastWindowsVista

                          // GetFileAttributesEx() / GetFileAttributesTransacted()
                          // 2013-01-13: MSDN confirms LongPath usage.

                  ? NativeMethods.GetFileAttributesEx(pathLp, NativeMethods.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, out win32AttrData)
                  : NativeMethods.GetFileAttributesTransacted(pathLp, NativeMethods.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, out win32AttrData, transaction.SafeHandle)))
                    {
                        lastError = Marshal.GetLastWin32Error();

                        switch ((uint)lastError)
                        {
                        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.

                            // In case someone latched onto the file. Take the perf hit only for failure.
                            return(FillAttributeInfoCore(transaction, pathLp, ref win32AttrData, true, returnErrorOnNotFound));
                        }


                        if (!returnErrorOnNotFound)
                        {
                            // Return default value for backward compatibility.
                            lastError = (int)Win32Errors.ERROR_SUCCESS;

                            win32AttrData.dwFileAttributes = NativeMethods.InvalidFileAttributes;
                        }
                    }
                }
            }

            return(lastError);
        }