public FileEnum(string path, FileEnumFlags flags) { m_flags = flags; m_path = path; m_openForReadHandle = FileSystemManager.AddToOpenListForRead(m_path); m_findFile = new NativeFindFile(m_path, "*"); }
public bool MoveNext() { if (m_disposed) throw new ObjectDisposedException(); NativeFileInfo fileinfo = m_findFile.GetNext(); while (fileinfo != null) { if (m_flags != FileEnumFlags.FilesAndDirectories) { uint targetAttribute = (0 != (m_flags & FileEnumFlags.Directories) ? (uint)FileAttributes.Directory : 0); if ((fileinfo.Attributes & (uint)FileAttributes.Directory) == targetAttribute) { m_currentFile = fileinfo; break; } } else { m_currentFile = fileinfo; break; } fileinfo = m_findFile.GetNext(); } if (fileinfo == null) { m_findFile.Close(); m_findFile = null; FileSystemManager.RemoveFromOpenList(m_openForReadHandle); m_openForReadHandle = null; } return fileinfo != null; }
public void Reset() { if (m_disposed) throw new ObjectDisposedException(); if (m_findFile != null) { m_findFile.Close(); } if(m_openForReadHandle == null) { m_openForReadHandle = FileSystemManager.AddToOpenListForRead(m_path); } m_findFile = new NativeFindFile(m_path, "*"); }
protected virtual void Dispose(bool disposing) { if (m_findFile != null) { m_findFile.Close(); m_findFile = null; } if (m_openForReadHandle != null) { FileSystemManager.RemoveFromOpenList(m_openForReadHandle); m_openForReadHandle = null; } m_disposed = true; }
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); } }