Beispiel #1
0
        public static void Delete(String path, bool recursive)
        {
            // Remove trailing directory separators.
            if (path != null)
            {
                int len = path.Length - 1;
                if (len > 0)
                {
                    if (path[len] == Path.DirectorySeparatorChar ||
                        path[len] == Path.AltDirectorySeparatorChar)
                    {
                        path = path.Substring(0, len);
                    }
                }
            }

            // Validate the pathname.
            ValidatePath(path);

            // Recursively delete the directory's contents if necessary.
            if (recursive)
            {
                InternalFileInfo[] entries;
                int    posn;
                String filename;
                if (DirMethods.GetFilesInDirectory(path, out entries)
                    == Errno.Success && entries != null)
                {
                    for (posn = 0; posn < entries.Length; ++posn)
                    {
                        filename = entries[posn].fileName;
                        if (filename == "." || filename == "..")
                        {
                            continue;
                        }
                        filename = path + Path.DirectorySeparatorChar +
                                   filename;
                        if (entries[posn].fileType == FileType.directory)
                        {
                            Delete(filename, true);
                        }
                        else
                        {
                            File.Delete(filename);
                        }
                    }
                }
            }

            // Delete the directory itself.
            HandleErrorsDir(DirMethods.Delete(path));
        }
Beispiel #2
0
        // Scan a directory to collect up all entries that match
        // the specified search criteria, returning FileSystemInfo's.
        internal static Object ScanDirectoryForInfos
            (String path, String searchPattern,
            ScanType scanType, Type arrayType)
        {
            Regex     regex;
            ArrayList list;

            InternalFileInfo[] entries;
            Errno    error;
            int      posn;
            String   filename;
            FileType type;

            // Get all files in the directory.
            error = DirMethods.GetFilesInDirectory(path, out entries);
            if (error != Errno.Success)
            {
                HandleErrorsDir(error);
            }
            if (entries == null)
            {
                return(new String [0]);
            }

            // Convert the search pattern into a regular expression.
            if (searchPattern == null)
            {
                regex = null;
            }
            else
            {
                regex = new Regex(searchPattern, RegexSyntax.Wildcard);
            }

            // Scan the file list and collect up matching entries.
            list = new ArrayList(entries.Length);
            for (posn = 0; posn < entries.Length; ++posn)
            {
                filename = entries[posn].fileName;
                if (filename == "." || filename == "..")
                {
                    continue;
                }
                type = entries[posn].fileType;
                switch (scanType)
                {
                case ScanType.Directories:
                {
                    if (type != FileType.directory)
                    {
                        continue;
                    }
                }
                break;

                case ScanType.Files:
                {
                    if (type == FileType.directory)
                    {
                        continue;
                    }
                }
                break;

                default: break;
                }
                if (regex != null && !regex.Match(filename))
                {
                    continue;
                }
                if (type == FileType.directory)
                {
                    list.Add(new DirectoryInfo
                                 (Path.Combine(path, filename)));
                }
                else
                {
                    list.Add(new FileInfo(Path.Combine(path, filename)));
                }
            }

            // Dispose of the regular expression.
            if (regex != null)
            {
                regex.Dispose();
            }

            // Return the list of strings to the caller.
            return(list.ToArray(arrayType));
        }