Beispiel #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);
        }
        public T Get <T>()
        {
            using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors))
            {
                NativeMethods.WIN32_FIND_DATA win32FindData;


                // Not explicitly set to be a folder.

                if (!IsDirectory)
                {
                    using (var handle = FindFirstFile(InputPath, out win32FindData))

                        return(null == handle

                     ? (T)(object)null

                     : NewFileSystemEntryType <T>((win32FindData.dwFileAttributes & FileAttributes.Directory) != 0, null, null, InputPath, win32FindData));
                }


                using (var handle = FindFirstFile(InputPath, out win32FindData, true))
                {
                    if (null == handle)
                    {
                        // InputPath might be a logical drive such as: "C:\", "D:\".

                        var attrs = new NativeMethods.WIN32_FILE_ATTRIBUTE_DATA();

                        var lastError = File.FillAttributeInfoCore(Transaction, Path.GetRegularPathCore(InputPath, GetFullPathOptions.None, false), ref attrs, false, true);
                        if (lastError != Win32Errors.NO_ERROR)
                        {
                            if (!ContinueOnException)
                            {
                                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.
                                case Win32Errors.ERROR_BAD_NET_NAME:

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

                                ThrowPossibleException((uint)lastError, InputPath);
                            }

                            return((T)(object)null);
                        }


                        win32FindData = new NativeMethods.WIN32_FIND_DATA
                        {
                            cFileName        = Path.CurrentDirectoryPrefix,
                            dwFileAttributes = attrs.dwFileAttributes,
                            ftCreationTime   = attrs.ftCreationTime,
                            ftLastAccessTime = attrs.ftLastAccessTime,
                            ftLastWriteTime  = attrs.ftLastWriteTime,
                            nFileSizeHigh    = attrs.nFileSizeHigh,
                            nFileSizeLow     = attrs.nFileSizeLow
                        };
                    }


                    VerifyInstanceType(win32FindData);
                }


                return(NewFileSystemEntryType <T>((win32FindData.dwFileAttributes & FileAttributes.Directory) != 0, null, null, InputPath, win32FindData));
            }
        }