Example #1
0
        public static ZlpFileInfo[] GetFiles(string directoryPath, string pattern, SearchOption searchOption)
        {
            directoryPath = CheckAddLongPathPrefix(directoryPath);

            var results = new List <ZlpFileInfo>();

            PInvokeHelper.WIN32_FIND_DATA findData;
            var findHandle = PInvokeHelper.FindFirstFile(directoryPath.TrimEnd('\\') + "\\" + pattern, out findData);

            if (findHandle != PInvokeHelper.INVALID_HANDLE_VALUE)
            {
                try
                {
                    bool found;
                    do
                    {
                        var currentFileName = findData.cFileName;

                        // if this is a file, find its contents
                        if (((int)findData.dwFileAttributes & PInvokeHelper.FILE_ATTRIBUTE_DIRECTORY) == 0)
                        {
                            results.Add(new ZlpFileInfo(ZlpPathHelper.Combine(directoryPath, currentFileName)));
                        }

                        // find next
                        found = PInvokeHelper.FindNextFile(findHandle, out findData);
                    } while (found);
                }
                finally
                {
                    // close the find handle
                    PInvokeHelper.FindClose(findHandle);
                }
            }

            if (searchOption == SearchOption.AllDirectories)
            {
                foreach (var dir in GetDirectories(directoryPath))
                {
                    results.AddRange(GetFiles(dir.FullName, pattern, searchOption));
                }
            }

            return(results.ToArray());
        }
Example #2
0
        public static ZlpDirectoryInfo[] GetDirectories(string directoryPath, string pattern)
        {
            directoryPath = CheckAddLongPathPrefix(directoryPath);

            var results = new List <ZlpDirectoryInfo>();

            PInvokeHelper.WIN32_FIND_DATA findData;
            var findHandle = PInvokeHelper.FindFirstFile(directoryPath.TrimEnd('\\') + @"\" + pattern, out findData);

            if (findHandle != PInvokeHelper.INVALID_HANDLE_VALUE)
            {
                try
                {
                    bool found;
                    do
                    {
                        var currentFileName = findData.cFileName;

                        // if this is a directory, find its contents
                        if (((int)findData.dwFileAttributes & PInvokeHelper.FILE_ATTRIBUTE_DIRECTORY) != 0)
                        {
                            if (currentFileName != @"." && currentFileName != @"..")
                            {
                                results.Add(new ZlpDirectoryInfo(ZlpPathHelper.Combine(directoryPath, currentFileName)));
                            }
                        }

                        // find next
                        found = PInvokeHelper.FindNextFile(findHandle, out findData);
                    } while (found);
                }
                finally
                {
                    // close the find handle
                    PInvokeHelper.FindClose(findHandle);
                }
            }

            return(results.ToArray());
        }