📄 directory.cs
字号:
/* * Directory.cs - Implementation of the "System.IO.Directory" class. * * Copyright (C) 2003 Southern Storm Software, Pty Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */namespace System.IO{using System;using System.Private;using System.Collections;using Platform;public sealed class Directory{ // Cannot instantiate this class. private Directory() {} // Validate a pathname. internal static void ValidatePath(String path) { if(path == null) { throw new ArgumentNullException("path"); } else if(path.Length == 0) { throw new ArgumentException(_("IO_InvalidPathname")); } else if(!FileMethods.ValidatePathname(path)) { throw new ArgumentException(_("IO_InvalidPathname")); } } // Handle errors reported by the runtime engine. private static void HandleErrorsDir(Errno error) { switch(error) { case Errno.Success: break; case Errno.ENOENT: case Errno.ENOTDIR: { throw new DirectoryNotFoundException (_("IO_DirNotFound")); } // Not reached. case Errno.EACCES: { throw new UnauthorizedAccessException (_("IO_AccessDenied")); } case Errno.ENAMETOOLONG: { throw new PathTooLongException (_("Exception_PathTooLong")); } default: { throw new IOException(error); } // Not reached. } } internal static void HandleErrorsFile(Errno error) { switch(error) { case Errno.Success: break; case Errno.ENOENT: { throw new FileNotFoundException (_("IO_PathNotFound")); } // Not reached. case Errno.ENOTDIR: { throw new DirectoryNotFoundException (_("IO_DirNotFound")); } // Not reached. case Errno.EACCES: { throw new UnauthorizedAccessException (_("IO_AccessDenied")); } case Errno.ENAMETOOLONG: { throw new PathTooLongException (_("Exception_PathTooLong")); } default: { throw new IOException(error); } // Not reached. } } // Delete a directory. public static void Delete(String path) { Delete(path, false); } 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)); } // Determine if a directory with a specific path exists. public static bool Exists(String path) { ValidatePath(path); return (FileMethods.GetFileType(path) == FileType.directory); } // Get the creation time of a directory. public static DateTime GetCreationTime(String path) { long time; ValidatePath(path); HandleErrorsDir(DirMethods.GetCreationTime(path, out time)); return (new DateTime(time)).ToLocalTime(); } // Get the current working directory. public static String GetCurrentDirectory() { String dir = DirMethods.GetCurrentDirectory(); if(dir == null) { throw new UnauthorizedAccessException (_("IO_AccessDenied")); } return dir; } // Get the current working directory for a particular drive. internal static String GetCurrentDirectory(char drive) { if(drive >= 'a' && drive <= 'z') { drive = (char)(drive - 'a' + 'A'); } String current = GetCurrentDirectory(); if(current.Length >= 2 && Path.IsVolumeSeparator(current[1])) { char d = current[0]; if(d >= 'a' && d <= 'z') { d = (char)(d - 'a' + 'A'); } if(d == drive) { return current; } } return drive.ToString() + ":" + Path.DirectorySeparatorChar.ToString(); } // Directory scan types. internal enum ScanType { Directories = 0, Files = 1, DirectoriesAndFiles = 2 }; // Scan a directory to collect up all entries that match // the specified search criteria. private static String[] ScanDirectory (String path, String searchPattern, ScanType scanType) { 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; } list.Add(Path.Combine(path, filename)); } // Dispose of the regular expression. if(regex != null) { regex.Dispose(); } // Return the list of strings to the caller. return (String[])(list.ToArray(typeof(String))); }#if !ECMA_COMPAT // Scan a directory to collect up all entries that match
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -