public DirectoryInfo[] GetDirectories(String searchPattern, SearchOption searchOption)
        {
            if (searchPattern == null)
            {
                throw new ArgumentNullException("searchPattern");
            }

            String[] dirNames        = Directory.InternalGetFileDirectoryNames(FullPath, OriginalPath, searchPattern, false, true, searchOption);
            String[] permissionNames = new String[dirNames.Length];

            // We need full path for permission check.
            // InternalGetFileDirectoryNames returns qualified user path,
            // i.e, path starts from OriginalPath. We need to convert this to fullpath.
            for (int i = 0; i < dirNames.Length; i++)
            {
                BCLDebug.Assert(!dirNames[i].EndsWith(Path.DirectorySeparatorChar), "InternalGetFileDirectoryNames returned paths should not have trailing slash!");

                dirNames[i]        = FixupFileDirFullPath(dirNames[i]);
                permissionNames[i] = dirNames[i] + "\\."; // these will never have a slash at end
            }
            if (dirNames.Length != 0)
            {
                new FileIOPermission(FileIOPermissionAccess.Read, permissionNames, false, false).Demand();
            }

            DirectoryInfo[] dirs = new DirectoryInfo[dirNames.Length];
            for (int i = 0; i < dirNames.Length; i++)
            {
                dirs[i] = new DirectoryInfo(dirNames[i], false);
            }
            return(dirs);
        }
        public FileInfo[] GetFiles(String searchPattern, SearchOption searchOption)
        {
            if (searchPattern == null)
            {
                throw new ArgumentNullException("searchPattern");
            }

            String[] fileNames = Directory.InternalGetFileDirectoryNames(FullPath, OriginalPath, searchPattern, true, false, searchOption);

            // We need full path for permission check.
            // InternalGetFileDirectoryNames returns qualified user path,
            // i.e, path starts from OriginalPath. We need to convert this to fullpath.
            for (int i = 0; i < fileNames.Length; i++)
            {
                fileNames[i] = FixupFileDirFullPath(fileNames[i]);
            }

            if (fileNames.Length != 0)
            {
                new FileIOPermission(FileIOPermissionAccess.Read, fileNames, false, false).Demand();
            }

            FileInfo[] files = new FileInfo[fileNames.Length];
            for (int i = 0; i < fileNames.Length; i++)
            {
                files[i] = new FileInfo(fileNames[i], false);
            }
            return(files);
        }
Beispiel #3
0
        // Returns an array of Directories in the current DirectoryInfo matching the
        // given search criteria (ie, "System*" could match the System & System32
        // directories).
        /// <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.GetDirectories1"]/*' />
        public DirectoryInfo[] GetDirectories(String searchPattern)
        {
            new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandDir, false, false).Demand();

            if (searchPattern == null)
            {
                throw new ArgumentNullException("searchPattern");
            }

            searchPattern = searchPattern.TrimEnd();
            if (searchPattern.Length == 0)
            {
                return(new DirectoryInfo[0]);
            }

            Path.CheckSearchPattern(searchPattern);
            String path = FullPath;


            String searchPath = Path.GetDirectoryName(searchPattern);

            if (searchPath != null && searchPath != String.Empty) // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
            {
                String demandPath = Path.InternalCombine(FullPath, searchPath);
                if (demandPath.EndsWith(Path.DirectorySeparatorChar))
                {
                    demandPath = demandPath + ".";
                }
                else
                {
                    demandPath = demandPath + Path.DirectorySeparatorChar + ".";
                }

                new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new String[] { demandPath }, false, false).Demand();
                path = Path.Combine(path, searchPath);
            }

            String fullPath = Path.InternalCombine(FullPath, searchPattern);

            String[] dirNames        = Directory.InternalGetFileDirectoryNames(fullPath, false);
            String[] permissionNames = new String[dirNames.Length];
            for (int i = 0; i < dirNames.Length; i++)
            {
                dirNames[i]        = Path.InternalCombine(path, dirNames[i]);
                permissionNames[i] = dirNames[i] + "\\.";                 // these will never have a slash at end
            }
            if (dirNames.Length != 0)
            {
                new FileIOPermission(FileIOPermissionAccess.Read, permissionNames, false, false).Demand();
            }

            DirectoryInfo[] dirs = new DirectoryInfo[dirNames.Length];
            for (int i = 0; i < dirNames.Length; i++)
            {
                dirs[i] = new DirectoryInfo(dirNames[i], false);
            }
            return(dirs);
        }
        // Returns an array of Directories in the current DirectoryInfo matching the
        // given search criteria (ie, "System*" could match the System & System32
        // directories).
        //| <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.GetDirectories1"]/*' />
        public DirectoryInfo[] GetDirectories(String searchPattern)
        {
            if (searchPattern == null)
            {
                throw new ArgumentNullException("searchPattern");
            }

            searchPattern = searchPattern.TrimEnd();
            if (searchPattern.Length == 0)
            {
                return(new DirectoryInfo[0]);
            }

            String path = FullPath;

            String searchPath = Path.GetDirectoryName(searchPattern);

            if (searchPath != null && searchPath != String.Empty) // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
            {
                path = Path.Combine(path, searchPath);
            }

            String fullPath = Path.InternalCombine(FullPath, searchPattern);

            String[] dirNames        = Directory.InternalGetFileDirectoryNames(fullPath, OriginalPath, false);
            String[] permissionNames = new String[dirNames.Length];
            for (int i = 0; i < dirNames.Length; i++)
            {
                dirNames[i]        = Path.InternalCombine(path, dirNames[i]);
                permissionNames[i] = dirNames[i] + "\\."; // these will never have a slash at end
            }
            DirectoryInfo[] dirs = new DirectoryInfo[dirNames.Length];
            for (int i = 0; i < dirNames.Length; i++)
            {
                dirs[i] = new DirectoryInfo((!)dirNames[i], false);
            }
            return(dirs);
        }