//------------------------------------------------------------------------------ // // System.IO.Directory.cs // // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved // // Author: Jim Richardson, develop@wtfo-guru.com // Created: Monday, August 13, 2001 // // TODO: Research exceptions for all methods //------------------------------------------------------------------------------ using System; using System.IO.Private; using System.Security.Permissions; namespace System.IO { /// /// /// public sealed class Directory : Object { /// /// Creates all directories not existing in path /// public static DirectoryInfo CreateDirectory(string path) { DirectoryInfo dInfo = getInfo(path); if(!dInfo.Exists) { dInfo.Create(); } return dInfo; } /// /// Delete an empty directory /// public static void Delete(string path) { DirectoryInfo dInfo = getInfo(path); if(dInfo.Exists) { dInfo.Delete(); } } /// /// Delete a directory, and contents if bRecurse is true /// public static void Delete(string path, bool bRecurse) { DirectoryInfo dInfo = getInfo(path); if(dInfo.Exists) { dInfo.Delete(bRecurse); } } /// /// Returns true if directory exists on disk /// public static bool Exists(string path) { return getInfo(path).Exists; } /// /// Returns the date and time the directory specified by path was created /// public static DateTime GetCreationTime(string path) { return getInfo(path).CreationTime; } /// /// Returns the date and time the directory specified by path was created /// public static string GetCurrentDirectory() { // Implementation complete 08/25/2001 14:24 except for // LAMESPEC: documentation specifies invalid exceptions (i think) // also shouldn't need Write to getcurrrent should we? string str = Environment.CurrentDirectory; CheckPermission.Demand(FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, str); return str; } /// /// Returns an array of directories in the directory specified by path /// public static string[] GetDirectories(string path) { return getNames(getInfo(path).GetDirectories()); } /// /// Returns an array of directories in the directory specified by path /// matching the filter specified by mask /// public static string[] GetDirectories(string path, string mask) { return getNames(DirectoryInfo.GetDirectories(mask)); } /// /// Returns the root of the specified path /// public static string GetDirectoryRoot(string path) { return getInfo(path).Root.FullName; } /// /// Returns an array of files in the directory specified by path /// public static string[] GetFiles(string path) { return getNames(getInfo(path).GetFiles()); } /// /// Returns an array of files in the directory specified by path /// matching the filter specified by mask /// public static string[] GetFiles(string path, string mask) { return getNames(getInfo(path).GetFiles()); } /// /// Returns an array of filesystementries in the directory specified by path /// I think this is just files and directories /// public static string[] GetFileSystemEntries(string path) { // TODO: Research to verify this is files + directories return getNames(getInfo(path).GetFileSystemInfos()); } /// /// Returns an array of filesystementries in the directory specified by path /// matching the filter specified by mask /// public static string[] GetFileSystemEntries(string path, string mask) { // TODO: Research to verify this is files + directories return getNames(getInfo(path).GetFileSystemInfos()); } /// /// Returns the date and time the directory specified by path was last accessed /// public static DateTime GetLastAccessTime(string path) { return getInfo(path).LastAccessTime; } /// /// Returns the date and time the directory specified by path was last modified /// public static DateTime GetLastWriteTime(string path) { return getInfo(path).LastWriteTime; } /// /// Returns an array of logical drives on this system /// public static string[] GetLogicalDrives() { // TODO: Implement return null; } /// /// Returns the parent directory of the directory specified by path /// public static DirectoryInfo GetParent(string path) { // TODO: Implement return null; } /// /// Moves a directory and its contents /// public static void Move(string src, string dst) { getInfo(src).MoveTo(dst); } /// /// Sets the creation time of the directory specified by path /// public static void SetCreationTime(string path, DateTime creationTime) { getInfo(path).CreationTime = creationTime; } /// /// Sets the current directory to the directory specified by path /// public static void SetCurrentDirectory(string path) { // Implementation complete 08/25/2001 14:24 except for // LAMESPEC: documentation specifies invalid exceptions IOException (i think) CheckArgument.Path(path, true); CheckPermission.Demand(FileIOPermissionAccess.Read & FileIOPermissionAccess.Write, path); if(!Exists(path)) { throw new DirectoryNotFoundException("Directory \"" + path + "\" not found."); } Environment.CurrentDirectory = path; } /// /// Sets the last access time of the directory specified by path /// public static void SetLastAccessTime(string path, DateTime accessTime) { getInfo(path).LastAccessTime = accessTime; } /// /// Sets the last write time of the directory specified by path /// public static void SetLastWriteTime(string path, DateTime modifiedTime) { getInfo(path).LastWriteTime = modifiedTime; } private static DirectoryInfo getInfo(string path) { return new DirectoryInfo(path); } private static string[] getNames(FileSystemInfo[] arInfo) { int index = 0; string[] ar = new string[arInfo.Length]; foreach(FileInfo fi in arInfo) { ar[index++] = fi.FullName; } return ar; } } }