private static string[] GetChildren(string path, string searchPattern, bool isDirectory) { // path and searchPattern validation in Path.GetFullPath() and Path.NormalizePath() path = Path.GetFullPath(path); if (!Directory.Exists(path)) throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); Path.NormalizePath(searchPattern, true); ArrayList fileNames = new ArrayList(); string root = Path.GetPathRoot(path); if (String.Equals(root, path)) { /// This is special case. Return all the volumes. /// Note this will not work, once we start having \\server\share like paths. if (isDirectory) { VolumeInfo[] volumes = VolumeInfo.GetVolumes(); int count = volumes.Length; for (int i = 0; i < count; i++) { fileNames.Add(volumes[i].RootDirectory); } } } else { Object record = FileSystemManager.AddToOpenListForRead(path); NativeFindFile ff = null; try { ff = new NativeFindFile(path, searchPattern); uint targetAttribute = (isDirectory ? (uint)FileAttributes.Directory : 0); NativeFileInfo fileinfo = ff.GetNext(); while (fileinfo != null) { if ((fileinfo.Attributes & (uint)FileAttributes.Directory) == targetAttribute) { fileNames.Add(fileinfo.FileName); } fileinfo = ff.GetNext(); } } finally { if (ff != null) ff.Close(); FileSystemManager.RemoveFromOpenList(record); } } return (String[])fileNames.ToArray(typeof(String)); }
public static void Delete(string path, bool recursive) { path = Path.GetFullPath(path); Object record = FileSystemManager.LockDirectory(path); try { uint attributes = NativeIO.GetAttributes(path); if (attributes == 0xFFFFFFFF) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } if (((attributes & (uint)(FileAttributes.Directory)) == 0) || ((attributes & (uint)(FileAttributes.ReadOnly)) != 0)) { /// it's readonly or not a directory throw new IOException("", (int)IOException.IOExceptionErrorCode.UnauthorizedAccess); } if (!Exists(path)) // make sure it is indeed a directory (and not a file) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotFound); } if (!recursive) { NativeFindFile ff = new NativeFindFile(path, "*"); try { if (ff.GetNext() != null) { throw new IOException("", (int)IOException.IOExceptionErrorCode.DirectoryNotEmpty); } } finally { ff.Close(); } } NativeIO.Delete(path); } finally { // regardless of what happened, we need to release the directory when we're done FileSystemManager.UnlockDirectory(record); } }