Esempio n. 1
0
        private static string[] GetChildren(string path, string searchPattern, bool isDirectory)
        {
            // path and searchPattern validation in Path.GetFullPath() and Path.NormalizePath()

            path = Path.GetFullPath(path);
            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)));
        }
Esempio n. 2
0
        public FileEnum(string path, FileEnumFlags flags)
        {
            m_flags = flags;
            m_path  = path;

            m_openForReadHandle = FileSystemManager.AddToOpenListForRead(m_path);
            m_findFile          = new NativeFindFile(m_path, "*");
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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;
        }
Esempio n. 5
0
        public void Refresh()
        {
            Object record = FileSystemManager.AddToOpenListForRead(m_fullPath);

            try
            {
                _nativeFileInfo = NativeFindFile.GetFileInfo(m_fullPath);

                if (_nativeFileInfo == null)
                {
                    IOException.IOExceptionErrorCode errorCode = (this is FileInfo) ? IOException.IOExceptionErrorCode.FileNotFound : IOException.IOExceptionErrorCode.DirectoryNotFound;
                    throw new IOException("", (int)errorCode);
                }
            }
            finally
            {
                FileSystemManager.RemoveFromOpenList(record);
            }
        }
Esempio n. 6
0
        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, "*");
        }
Esempio n. 7
0
        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);
        }