Пример #1
0
        // Delete a file.
        public static void Delete(String path)
        {
            Directory.ValidatePath(path);
            Errno errno = DirMethods.Delete(path);

            if (errno != Errno.ENOENT)
            {
                Directory.HandleErrorsFile(errno);
            }
        }
Пример #2
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));
        }