Пример #1
0
        // Returns an array of strongly typed FileSystemInfo entries in the path with the
        // given search criteria (ie, "*.txt").
        /// <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.GetFileSystemInfos"]/*' />
        public FileSystemInfo[] GetFileSystemInfos(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 FileSystemInfo[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( '\\' ))
                    demandPath = demandPath + ".";
                else
                    demandPath = demandPath + "\\.";

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

            String [] dirNames = Directory.InternalGetDirectories(FullPath, OriginalPath, searchPattern);
            String [] fileNames = Directory.InternalGetFiles(FullPath, OriginalPath, searchPattern);
            FileSystemInfo [] fileSystemEntries = new FileSystemInfo[dirNames.Length + fileNames.Length];
            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] +  "\\.";
            }
            if (dirNames.Length != 0)
                new FileIOPermission(FileIOPermissionAccess.Read,permissionNames,false,false).Demand();
		   
            for (int i = 0;i<fileNames.Length;i++)
                fileNames[i] = Path.InternalCombine(path, fileNames[i]);
            if (fileNames.Length != 0)
                new FileIOPermission(FileIOPermissionAccess.Read,fileNames,false,false).Demand();
		   
            int count = 0;
            for (int i = 0;i<dirNames.Length;i++)
                fileSystemEntries[count++] = new DirectoryInfo(dirNames[i], false);
		   
            for (int i = 0;i<fileNames.Length;i++)
                fileSystemEntries[count++] = new FileInfo(fileNames[i], false);
		   
            return fileSystemEntries;
        }
Пример #2
0
        // Returns an array of strongly typed FileSystemInfo entries in the path with the
        // given search criteria (ie, "*.txt").
        //| <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.GetFileSystemInfos"]/*' />
        public FileSystemInfo[] GetFileSystemInfos(String searchPattern)
        {
            if (searchPattern == null)
            {
                throw new ArgumentNullException("searchPattern");
            }

            searchPattern = searchPattern.TrimEnd();
            if (searchPattern.Length == 0)
            {
                return(new FileSystemInfo[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 []         dirNames          = Directory.InternalGetDirectories(FullPath, OriginalPath, searchPattern);
            String []         fileNames         = Directory.InternalGetFiles(FullPath, OriginalPath, searchPattern);
            FileSystemInfo [] fileSystemEntries = new FileSystemInfo[dirNames.Length + fileNames.Length];
            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] + "\\.";
            }

            for (int i = 0; i < fileNames.Length; i++)
            {
                fileNames[i] = Path.InternalCombine(path, fileNames[i]);
            }

            int count = 0;

            for (int i = 0; i < dirNames.Length; i++)
            {
                fileSystemEntries[count++] = new DirectoryInfo((!)dirNames[i], false);
            }

            for (int i = 0; i < fileNames.Length; i++)
            {
                fileSystemEntries[count++] = new FileInfo((!)fileNames[i], false);
            }

            return(fileSystemEntries);
        }