Example #1
0
        private SafeFindFileHandle FindFirstFile(string pathLp, out NativeMethods.Win32FindData win32FindData)
        {
            SafeFindFileHandle handle = Transaction == null || !NativeMethods.IsAtLeastWindowsVista

                                        // FindFirstFileEx() / FindFirstFileTransacted()
                                        // In the ANSI version of this function, the name is limited to MAX_PATH characters.
                                        // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
                                        // 2013-01-13: MSDN confirms LongPath usage.

                                        // A trailing backslash is not allowed.
            ? NativeMethods.FindFirstFileEx(Path.RemoveTrailingDirectorySeparator(pathLp, false), FindExInfoLevel, out win32FindData, _limitSearchToDirs, IntPtr.Zero, LargeCache)
            : NativeMethods.FindFirstFileTransacted(Path.RemoveTrailingDirectorySeparator(pathLp, false), FindExInfoLevel, out win32FindData, _limitSearchToDirs, IntPtr.Zero, LargeCache, Transaction.SafeHandle);

            if (handle.IsInvalid)
            {
                int lastError = Marshal.GetLastWin32Error();
                handle.Close();

                if (!ContinueOnException)
                {
                    // Use path without any search filter.
                    string path = Path.GetDirectoryName(pathLp, false);

                    switch ((uint)lastError)
                    {
                    case Win32Errors.ERROR_FILE_NOT_FOUND:
                    case Win32Errors.ERROR_PATH_NOT_FOUND:
                        // MSDN: .NET 3.5+: DirectoryNotFoundException: Path is invalid, such as referring to an unmapped drive.
                        if (lastError == Win32Errors.ERROR_FILE_NOT_FOUND)
                        {
                            lastError = (int)Win32Errors.ERROR_PATH_NOT_FOUND;
                        }
                        NativeError.ThrowException(lastError, path);
                        break;

                    case Win32Errors.ERROR_DIRECTORY:
                        // MSDN: .NET 3.5+: IOException: path is a file name.
                        NativeError.ThrowException(lastError, path);
                        break;

                    case Win32Errors.ERROR_ACCESS_DENIED:
                        // MSDN: .NET 3.5+: UnauthorizedAccessException: The caller does not have the required permission.
                        NativeError.ThrowException(lastError, path);
                        break;
                    }

                    // MSDN: .NET 3.5+: IOException
                    NativeError.ThrowException(lastError, path);
                }
            }

            return(handle);
        }
Example #2
0
        private T NewFileSystemEntryType <T>(NativeMethods.Win32FindData win32FindData, string fullPathLp, bool isFolder)
        {
            // Determine yield.
            if (FileSystemObjectType != null && ((!(bool)FileSystemObjectType || !isFolder) && (!(bool)!FileSystemObjectType || isFolder)))
            {
                return((T)(object)null);
            }

            if (AsString)
            {
                // Return object instance FullPath property as string, optionally in Unicode format.
                return((T)(object)(AsLongPath ? fullPathLp : Path.GetRegularPathInternal(fullPathLp, GetFullPathOptions.None)));
            }


            // Make sure the requested file system object type is returned.
            // null = Return files and directories.
            // true = Return only directories.
            // false = Return only files.

            var fsei = new FileSystemEntryInfo(win32FindData)
            {
                FullPath = Path.GetRegularPathInternal(fullPathLp, GetFullPathOptions.None)
            };

            return(AsFileSystemInfo
                   // Return object instance of type FileSystemInfo.
            ? (T)(object)(fsei.IsDirectory
               ? (FileSystemInfo)
                          new DirectoryInfo(Transaction, fsei.LongFullPath, PathFormat.LongFullPath)
            {
                EntryInfo = fsei
            }
               : new FileInfo(Transaction, fsei.LongFullPath, PathFormat.LongFullPath)
            {
                EntryInfo = fsei
            })

                   // Return object instance of type FileSystemEntryInfo.
            : (T)(object)fsei);
        }
Example #3
0
 /// <summary>Initializes a new instance of the <see cref="T:FileSystemEntryInfo"/> class.</summary>
 /// <param name="findData">The <see cref="NativeMethods.Win32FindData"/> structure.</param>
 internal FileSystemEntryInfo(NativeMethods.Win32FindData findData)
 {
     _win32FindData = findData;
 }