Exemplo n.º 1
0
        private static bool IsValidWindowsFileName(string fileName)
        {
            string fileNameNoExt   = LongPath.GetFileNameWithoutExtension(fileName);
            string fileNameWithExt = LongPath.GetFileName(fileName);

            if (Array.Exists <string>(ReservedBaseFileNames, delegate(string s) { return(fileNameNoExt.Equals(s, StringComparison.OrdinalIgnoreCase)); }))
            {
                return(false);
            }

            if (Array.Exists <string>(ReservedFileNames, delegate(string s) { return(fileNameWithExt.Equals(s, StringComparison.OrdinalIgnoreCase)); }))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(fileNameWithExt))
            {
                return(false);
            }

            bool allDotsOrWhiteSpace = true;

            for (int i = 0; i < fileName.Length; ++i)
            {
                if (fileName[i] != '.' && !char.IsWhiteSpace(fileName[i]))
                {
                    allDotsOrWhiteSpace = false;
                    break;
                }
            }

            if (allDotsOrWhiteSpace)
            {
                return(false);
            }

            return(true);
        }
        public static IEnumerable <string> EnumerateFileSystemEntries(string path, string searchPattern, SearchOption searchOption, FilesOrDirectory filter)
        {
#if DOTNET5_4
            return(Directory.EnumerateFileSystemEntries(path, searchPattern, searchOption));
#else
            NativeMethods.WIN32_FIND_DATA findData;
            NativeMethods.SafeFindHandle  findHandle;
            string currentPath = null;
            int    errorCode   = 0;

            Queue <string> folders    = new Queue <string>();
            String         searchPath = LongPath.Combine(path, searchPattern);
            path          = LongPath.GetDirectoryName(searchPath);
            searchPattern = LongPath.GetFileName(searchPath);
            folders.Enqueue(path);
            while (folders.Count > 0)
            {
                currentPath = folders.Dequeue();
                if (searchOption == SearchOption.AllDirectories)
                {
                    findHandle = NativeMethods.FindFirstFileW(LongPath.Combine(LongPath.ToUncPath(currentPath), "*"), out findData);
                    if (!findHandle.IsInvalid)
                    {
                        do
                        {
                            if (findData.FileName != "." &&
                                findData.FileName != "..")
                            {
                                if (findData.FileAttributes == FileAttributes.Directory)
                                {
                                    folders.Enqueue(LongPath.Combine(currentPath, findData.FileName));
                                }
                            }
                        }while (NativeMethods.FindNextFileW(findHandle, out findData));

                        // Get last Win32 error right after native calls.
                        // Dispose SafeFindHandle will call native methods, it is possible to set last Win32 error.
                        errorCode = Marshal.GetLastWin32Error();
                        if (findHandle != null &&
                            !findHandle.IsInvalid)
                        {
                            findHandle.Dispose();
                        }
                        NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(errorCode,
                                                                              new int[] {
                            NativeMethods.ERROR_SUCCESS,
                            NativeMethods.ERROR_NO_MORE_FILES,
                            NativeMethods.ERROR_FILE_NOT_FOUND
                        });
                    }
                    else
                    {
                        NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] {
                            NativeMethods.ERROR_SUCCESS,
                            NativeMethods.ERROR_NO_MORE_FILES,
                            NativeMethods.ERROR_FILE_NOT_FOUND
                        });
                    }
                }

                findHandle = NativeMethods.FindFirstFileW(LongPath.Combine(LongPath.ToUncPath(currentPath), searchPattern), out findData);
                if (!findHandle.IsInvalid)
                {
                    do
                    {
                        if (findData.FileName != "." &&
                            findData.FileName != "..")
                        {
                            if ((filter == FilesOrDirectory.All) ||
                                (filter == FilesOrDirectory.Directory && findData.FileAttributes == FileAttributes.Directory) ||
                                (filter == FilesOrDirectory.File && findData.FileAttributes != FileAttributes.Directory))
                            {
                                yield return(LongPath.Combine(currentPath, findData.FileName));
                            }
                        }
                    }while (NativeMethods.FindNextFileW(findHandle, out findData));

                    // Get last Win32 error right after native calls.
                    // Dispose SafeFindHandle will call native methods, it is possible to set last Win32 error.
                    errorCode = Marshal.GetLastWin32Error();
                    if (findHandle != null &&
                        !findHandle.IsInvalid)
                    {
                        findHandle.Dispose();
                    }
                    NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(errorCode,
                                                                          new int[] {
                        NativeMethods.ERROR_SUCCESS,
                        NativeMethods.ERROR_NO_MORE_FILES,
                        NativeMethods.ERROR_FILE_NOT_FOUND
                    });
                }
                else
                {
                    NativeMethods.ThrowExceptionForLastWin32ErrorIfExists(new int[] {
                        NativeMethods.ERROR_SUCCESS,
                        NativeMethods.ERROR_NO_MORE_FILES,
                        NativeMethods.ERROR_FILE_NOT_FOUND
                    });
                }
            }
#endif
        }