Exemple #1
0
        // Returns an array of Directories in the current DirectoryInfo matching the
        // given search criteria (ie, "*.txt").
        //| <include file='doc\Directory.uex' path='docs/doc[@for="Directory.GetDirectories1"]/*' />
        public static String[] GetDirectories(String path, String searchPattern)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

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

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

            // Must fully qualify the path for the security check
            String fullPath = Path.GetFullPathInternal(path);

            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);            // Need to add the searchPath to return correct path and for right security checks
            }

            String [] dirNames = InternalGetDirectories(fullPath, path, searchPattern);
            for (int i = 0; i < dirNames.Length; i++)
            {
                dirNames[i] = Path.InternalCombine(path, dirNames[i]);
            }
            return(dirNames);
        }
Exemple #2
0
        // Returns an array of Directories in the current DirectoryInfo matching the
        // given search criteria (ie, "*.txt").
        /// <include file='doc\Directory.uex' path='docs/doc[@for="Directory.GetDirectories1"]/*' />
        public static String[] GetDirectories(String path, String searchPattern)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

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

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

            Path.CheckSearchPattern(searchPattern);

            // Must fully qualify the path for the security check
            String fullPath = Path.GetFullPathInternal(path);
            String demandPath;

            if (fullPath.EndsWith(Path.DirectorySeparatorChar))
            {
                demandPath = fullPath + ".";
            }
            else
            {
                demandPath = fullPath + Path.DirectorySeparatorChar + ".";
            }

            new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new String[] { demandPath }, false, false).Demand();

            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.
            {
                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); // Need to add the searchPath to return correct path and for right security checks
            }

            String [] dirNames = InternalGetDirectories(fullPath, searchPattern);
            for (int i = 0; i < dirNames.Length; i++)
            {
                dirNames[i] = Path.InternalCombine(path, dirNames[i]);
            }
            return(dirNames);
        }
Exemple #3
0
        // Internal helper function with no security checks
        // Note - fileNames returned by InternalGetFiles are not fully qualified.
        internal static String[] InternalGetFiles(String path, String searchPattern)
        {
            String fullPath = Path.InternalCombine(path, searchPattern);

            String[] fileNames = InternalGetFileDirectoryNames(fullPath, true);
            return(fileNames);
        }
Exemple #4
0
        private void CommonInit()
        {
            Contract.Assert(searchCriteria != null && searchData != null, "searchCriteria and searchData should be initialized");

            // Execute searchCriteria against the current directory
            String searchPath = Path.InternalCombine(searchData.fullPath, searchCriteria);

            Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();

            // Open a Find handle
#if MONO
            int error;
            _hnd = new SafeFindHandle(MonoIO.FindFirstFile(searchPath, out data.cFileName, out data.dwFileAttributes, out error));
#else
            _hnd = Win32Native.FindFirstFile(searchPath, data);
#endif

            if (_hnd.IsInvalid)
            {
#if MONO
                int hr = error;
#else
                int hr = Marshal.GetLastWin32Error();
#endif
                if (hr != Win32Native.ERROR_FILE_NOT_FOUND && hr != Win32Native.ERROR_NO_MORE_FILES)
                {
                    HandleError(hr, searchData.fullPath);
                }
                else
                {
                    // flag this as empty only if we're searching just top directory
                    // Used in fast path for top directory only
                    empty = searchData.searchOption == SearchOption.TopDirectoryOnly;
                }
            }
            // fast path for TopDirectoryOnly. If we have a result, go ahead and set it to
            // current. If empty, dispose handle.
            if (searchData.searchOption == SearchOption.TopDirectoryOnly)
            {
                if (empty)
                {
                    _hnd.Dispose();
                }
                else
                {
                    SearchResult searchResult = CreateSearchResult(searchData, data);
                    if (_resultHandler.IsResultIncluded(searchResult))
                    {
                        current = _resultHandler.CreateObject(searchResult);
                    }
                }
            }
            // for AllDirectories, we first recurse into dirs, so cleanup and add searchData
            // to the stack
            else
            {
                _hnd.Dispose();
                searchStack.Add(searchData);
            }
        }
Exemple #5
0
        // Internal helper function that has no security checks
        // Note - InternalGetDirectories returns non-qualified directory names.
        internal static String[] InternalGetDirectories(String path, String searchPattern)
        {
            String fullPath = Path.InternalCombine(path, searchPattern);

            String[] dirNames = InternalGetFileDirectoryNames(fullPath, false);
            return(dirNames);
        }
        [System.Security.SecurityCritical]  // auto-generated
        private DirectoryInfo CreateSubdirectoryHelper(String path, Object directorySecurity)
        {
            Contract.Requires(path != null);

            String newDirs  = Path.InternalCombine(FullPath, path);
            String fullPath = Path.GetFullPathInternal(newDirs);

            if (0 != String.Compare(FullPath, 0, fullPath, 0, FullPath.Length, StringComparison.OrdinalIgnoreCase))
            {
                String displayPath = __Error.GetDisplayablePath(DisplayPath, false);
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSubPath", path, displayPath));
            }

            // Ensure we have permission to create this subdirectory.
            String demandDirForCreation = Directory.GetDemandDir(fullPath, true);

#if FEATURE_CORECLR
            FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Write, OriginalPath, demandDirForCreation);
            state.EnsureState();
#else
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { demandDirForCreation }, false, false).Demand();
#endif

            Directory.InternalCreateDirectory(fullPath, path, directorySecurity);

            // Check for read permission to directory we hand back by calling this constructor.
            return(new DirectoryInfo(fullPath));
        }
        private SearchResult CreateSearchResult(Directory.SearchData localSearchData, Win32Native.WIN32_FIND_DATA findData)
        {
            String userPathFinal = Path.InternalCombine(localSearchData.userPath, findData.cFileName);
            String fullPathFinal = Path.InternalCombine(localSearchData.fullPath, findData.cFileName);

            return(new SearchResult(fullPathFinal, userPathFinal, findData));
        }
        public DirectoryInfo CreateSubdirectory(String path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            String newDirs  = Path.InternalCombine(FullPath, path);
            String fullPath = Path.GetFullPathInternal(newDirs);

            if (0 != String.Compare(FullPath, 0, fullPath, 0, FullPath.Length, StringComparison.OrdinalIgnoreCase))
            {
                String displayPath = __Error.GetDisplayablePath(OriginalPath, false);
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidSubPath"), path, displayPath));
            }

            // Ensure we have permission to create this subdirectory.
            String demandDir = Directory.GetDemandDir(fullPath, true);

            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { demandDir }, false, false).Demand();

            Directory.InternalCreateDirectory(fullPath, path);
            // Check for read permission to directory we hand back by calling this constructor.
            return(new DirectoryInfo(fullPath));
        }
        // Returns an array of Files in the current DirectoryInfo matching the
        // given search criteria (ie, "*.txt").
        //| <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.GetFiles"]/*' />
        public FileInfo[] GetFiles(String searchPattern)
        {
            if (searchPattern == null)
            {
                throw new ArgumentNullException("searchPattern");
            }

            searchPattern = searchPattern.TrimEnd();
            if (searchPattern.Length == 0)
            {
                return(new FileInfo[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[] fileNames = Directory.InternalGetFiles(FullPath, OriginalPath, searchPattern);
            for (int i = 0; i < fileNames.Length; i++)
            {
                fileNames[i] = Path.InternalCombine(path, fileNames[i]);
            }
            FileInfo[] files = new FileInfo[fileNames.Length];
            for (int i = 0; i < fileNames.Length; i++)
            {
                files[i] = new FileInfo((!)fileNames[i], false);
            }
            return(files);
        }
        // Converts the fully qualified user path returned by InternalGetFileDirectoryNames
        // into fullpath by combining the relevant portion of it with the fullpath of this directory
        // For ex, converts  foo\bar.txt into 'c:\temp\foo\bar.txt', where FullPath is 'c:\temp\foo'
        // and OriginalPath (aka userpath) is 'foo'
        private string FixupFileDirFullPath(String fileDirUserPath)
        {
            BCLDebug.Assert(fileDirUserPath != null, "InternalGetFileDirectoryNames returned paths should not be null!");
            BCLDebug.Assert(fileDirUserPath.StartsWith(OriginalPath, StringComparison.Ordinal), "InternalGetFileDirectoryNames returned paths should start with user path!");

            String fileDirFullPath;

            if (OriginalPath.Length == 0)
            {
                fileDirFullPath = Path.InternalCombine(FullPath, fileDirUserPath);
            }
            else if (OriginalPath.EndsWith(Path.DirectorySeparatorChar) || OriginalPath.EndsWith(Path.AltDirectorySeparatorChar))
            {
                BCLDebug.Assert((fileDirUserPath[OriginalPath.Length - 1] == Path.DirectorySeparatorChar) ||
                                (fileDirUserPath[OriginalPath.Length - 1] == Path.AltDirectorySeparatorChar), "InternalGetFileDirectoryNames returned incorrect user path!");
                fileDirFullPath = Path.InternalCombine(FullPath, fileDirUserPath.Substring(OriginalPath.Length));
            }
            else
            {
                BCLDebug.Assert((fileDirUserPath[OriginalPath.Length] == Path.DirectorySeparatorChar), "InternalGetFileDirectoryNames returned incorrect user path!");
                fileDirFullPath = Path.InternalCombine(FullPath, fileDirUserPath.Substring(OriginalPath.Length + 1));
            }

            return(fileDirFullPath);
        }
Exemple #11
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);
        }
        internal static string InternalCombine(string path1, string path2)
        {
            bool   flag;
            string path = Path.InternalCombine(TryRemoveLongPathPrefix(path1, out flag), path2);

            if (flag)
            {
                path = Path.AddLongPathPrefix(path);
            }
            return(path);
        }
        // Token: 0x060018A4 RID: 6308 RVA: 0x00050A94 File Offset: 0x0004EC94
        private static string GetFullSearchString(string fullPath, string searchPattern)
        {
            string text = Path.InternalCombine(fullPath, searchPattern);
            char   c    = text[text.Length - 1];

            if (Path.IsDirectorySeparator(c) || c == Path.VolumeSeparatorChar)
            {
                text += "*";
            }
            return(text);
        }
Exemple #14
0
        private static string GetFullSearchString(string fullPath, string searchPattern)
        {
            string str = Path.InternalCombine(fullPath, searchPattern);
            char   c   = str[str.Length - 1];

            if (!Path.IsDirectorySeparator(c) && (c != Path.VolumeSeparatorChar))
            {
                return(str);
            }
            return(str + '*');
        }
Exemple #15
0
        internal static string InternalCombine(string path1, string path2)
        {
            bool   removed;
            string path = Path.InternalCombine(LongPath.TryRemoveLongPathPrefix(path1, out removed), path2);

            if (removed)
            {
                path = Path.AddLongPathPrefix(path);
            }
            return(path);
        }
Exemple #16
0
        // Token: 0x06001ADF RID: 6879 RVA: 0x0005A51C File Offset: 0x0005871C
        internal static string InternalCombine(string path1, string path2)
        {
            bool   flag;
            string path3 = LongPath.TryRemoveLongPathPrefix(path1, out flag);
            string text  = Path.InternalCombine(path3, path2);

            if (flag)
            {
                text = Path.AddLongPathPrefix(text);
            }
            return(text);
        }
Exemple #17
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;
        }
        // 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);
        }
Exemple #19
0
        private static string GetFullSearchString(string fullPath, string searchPattern)
        {
            string str1  = Path.InternalCombine(fullPath, searchPattern);
            string str2  = str1;
            int    index = str2.Length - 1;
            char   c     = str2[index];

            if (Path.IsDirectorySeparator(c) || (int)c == (int)Path.VolumeSeparatorChar)
            {
                str1 += "*";
            }
            return(str1);
        }
Exemple #20
0
        /// <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.CreateSubdirectory"]/*' />
        public DirectoryInfo CreateSubdirectory(String path) {
            if (path==null)
                throw new ArgumentNullException("path");
			
            String newDirs = Path.InternalCombine(FullPath, path);
			String fullPath = Path.GetFullPathInternal(newDirs);

			if (0!=String.Compare(FullPath,0,fullPath,0, FullPath.Length,true, CultureInfo.InvariantCulture))
				throw new ArgumentException(String.Format(Environment.GetResourceString("Argument_InvalidSubPath"),path,OriginalPath));
			
            Directory.InternalCreateDirectory(fullPath,path);
			return new DirectoryInfo(fullPath, false);
        }
Exemple #21
0
        private void AddSearchableDirsToStack(Directory.SearchData localSearchData)
        {
            string         fileName    = Path.InternalCombine(localSearchData.fullPath, "*");
            SafeFindHandle hndFindFile = (SafeFindHandle)null;

            Win32Native.WIN32_FIND_DATA wiN32FindData = new Win32Native.WIN32_FIND_DATA();
            try
            {
                hndFindFile = Win32Native.FindFirstFile(fileName, wiN32FindData);
                if (hndFindFile.IsInvalid)
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    switch (lastWin32Error)
                    {
                    case 2:
                        return;

                    case 18:
                        return;

                    case 3:
                        return;

                    default:
                        this.HandleError(lastWin32Error, localSearchData.fullPath);
                        break;
                    }
                }
                int num1 = 0;
                do
                {
                    if (FileSystemEnumerableHelpers.IsDir(wiN32FindData))
                    {
                        string               fullPath     = Path.InternalCombine(localSearchData.fullPath, wiN32FindData.cFileName);
                        string               str          = Path.InternalCombine(localSearchData.userPath, wiN32FindData.cFileName);
                        SearchOption         searchOption = localSearchData.searchOption;
                        string               userPath     = str;
                        int                  num2         = (int)searchOption;
                        Directory.SearchData searchData   = new Directory.SearchData(fullPath, userPath, (SearchOption)num2);
                        this.searchStack.Insert(num1++, searchData);
                    }
                }while (Win32Native.FindNextFile(hndFindFile, wiN32FindData));
            }
            finally
            {
                if (hndFindFile != null)
                {
                    hndFindFile.Dispose();
                }
            }
        }
        private DirectoryInfo CreateSubdirectoryHelper(string path, object directorySecurity)
        {
            string fullPathInternal = Path.GetFullPathInternal(Path.InternalCombine(base.FullPath, path));

            if (string.Compare(base.FullPath, 0, fullPathInternal, 0, base.FullPath.Length, StringComparison.OrdinalIgnoreCase) != 0)
            {
                string displayablePath = __Error.GetDisplayablePath(base.DisplayPath, false);
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSubPath", new object[] { path, displayablePath }));
            }
            string demandDir = Directory.GetDemandDir(fullPathInternal, true);

            new FileIOPermission(FileIOPermissionAccess.Write, new string[] { demandDir }, false, false).Demand();
            Directory.InternalCreateDirectory(fullPathInternal, path, directorySecurity);
            return(new DirectoryInfo(fullPathInternal));
        }
        private static String GetFullSearchString(String fullPath, String searchPattern)
        {
            Contract.Requires(fullPath != null);
            Contract.Requires(searchPattern != null);

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

            // If path ends in a trailing slash (\), append a * or we'll get a "Cannot find the file specified" exception
            char lastChar = tempStr[tempStr.Length - 1];

            if (Path.IsDirectorySeparator(lastChar) || lastChar == Path.VolumeSeparatorChar)
            {
                tempStr = tempStr + '*';
            }

            return(tempStr);
        }
        //| <include file='doc\DirectoryInfo.uex' path='docs/doc[@for="DirectoryInfo.CreateSubdirectory"]/*' />
        public DirectoryInfo CreateSubdirectory(String path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            String newDirs  = Path.InternalCombine(FullPath, path);
            String fullPath = Path.GetFullPathInternal(newDirs);

            if (0 != String.Compare(FullPath, 0, fullPath, 0, FullPath.Length, true))
            {
                throw new ArgumentException(String.Format("Argument_InvalidSubPath", path, OriginalPath));
            }

            Directory.InternalCreateDirectory(fullPath, path);
            return(new DirectoryInfo(fullPath, false));
        }
        internal static String InternalCombine(String path1, String path2)
        {
            Contract.Requires(path1 != null);
            Contract.Requires(path2 != null);
            Contract.Requires(path2.Length != 0);
            Contract.Requires(!IsPathRooted(path2));

            bool   removedPrefix;
            String tempPath1 = TryRemoveLongPathPrefix(path1, out removedPrefix);

            String tempResult = Path.InternalCombine(tempPath1, path2);

            if (removedPrefix)
            {
                tempResult = Path.AddLongPathPrefix(tempResult);
            }
            return(tempResult);
        }
Exemple #26
0
        private void CommonInit()
        {
            string fileName = Path.InternalCombine(this.searchData.fullPath, this.searchCriteria);

            Win32Native.WIN32_FIND_DATA wiN32FindData = new Win32Native.WIN32_FIND_DATA();
            this._hnd = Win32Native.FindFirstFile(fileName, wiN32FindData);
            if (this._hnd.IsInvalid)
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                switch (lastWin32Error)
                {
                case 2:
                case 18:
                    this.empty = this.searchData.searchOption == SearchOption.TopDirectoryOnly;
                    break;

                default:
                    this.HandleError(lastWin32Error, this.searchData.fullPath);
                    break;
                }
            }
            if (this.searchData.searchOption == SearchOption.TopDirectoryOnly)
            {
                if (this.empty)
                {
                    this._hnd.Dispose();
                }
                else
                {
                    SearchResult searchResult = this.CreateSearchResult(this.searchData, wiN32FindData);
                    if (!this._resultHandler.IsResultIncluded(searchResult))
                    {
                        return;
                    }
                    this.current = this._resultHandler.CreateObject(searchResult);
                }
            }
            else
            {
                this._hnd.Dispose();
                this.searchStack.Add(this.searchData);
            }
        }
        private void AddSearchableDirsToStack(Directory.SearchData localSearchData)
        {
            string         fileName       = Path.InternalCombine(localSearchData.fullPath, "*");
            SafeFindHandle safeFindHandle = null;

            Win32Native.WIN32_FIND_DATA win32_FIND_DATA = default(Win32Native.WIN32_FIND_DATA);
            try
            {
                safeFindHandle = Win32Native.FindFirstFile(fileName, ref win32_FIND_DATA);
                if (safeFindHandle.IsInvalid)
                {
                    int lastWin32Error = Marshal.GetLastWin32Error();
                    if (lastWin32Error == 2 || lastWin32Error == 18 || lastWin32Error == 3)
                    {
                        return;
                    }
                    this.HandleError(lastWin32Error, localSearchData.fullPath);
                }
                int num = 0;
                do
                {
                    if (win32_FIND_DATA.IsNormalDirectory)
                    {
                        string               cFileName    = win32_FIND_DATA.cFileName;
                        string               text         = Path.CombineNoChecks(localSearchData.fullPath, cFileName);
                        string               text2        = Path.CombineNoChecks(localSearchData.userPath, cFileName);
                        SearchOption         searchOption = localSearchData.searchOption;
                        Directory.SearchData item         = new Directory.SearchData(text, text2, searchOption);
                        this.searchStack.Insert(num++, item);
                    }
                }while (Win32Native.FindNextFile(safeFindHandle, ref win32_FIND_DATA));
            }
            finally
            {
                if (safeFindHandle != null)
                {
                    safeFindHandle.Dispose();
                }
            }
        }
        // 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);
        }
        private void CommonInit()
        {
            string fileName = Path.InternalCombine(this.searchData.fullPath, this.searchCriteria);

            Win32Native.WIN32_FIND_DATA win32_FIND_DATA = default(Win32Native.WIN32_FIND_DATA);
            this._hnd = Win32Native.FindFirstFile(fileName, ref win32_FIND_DATA);
            if (this._hnd.IsInvalid)
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error != 2 && lastWin32Error != 18)
                {
                    this.HandleError(lastWin32Error, this.searchData.fullPath);
                }
                else
                {
                    this.empty = (this.searchData.searchOption == SearchOption.TopDirectoryOnly);
                }
            }
            if (this.searchData.searchOption == SearchOption.TopDirectoryOnly)
            {
                if (this.empty)
                {
                    this._hnd.Dispose();
                    return;
                }
                if (this._resultHandler.IsResultIncluded(ref win32_FIND_DATA))
                {
                    this.current = this._resultHandler.CreateObject(this.searchData, ref win32_FIND_DATA);
                    return;
                }
            }
            else
            {
                this._hnd.Dispose();
                this.searchStack.Add(this.searchData);
            }
        }
        [System.Security.SecurityCritical]  // auto-generated
        private void AddSearchableDirsToStack(Directory.SearchData localSearchData)
        {
            Contract.Requires(localSearchData != null);

            String         searchPath = Path.InternalCombine(localSearchData.fullPath, "*");
            SafeFindHandle hnd        = null;

            Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();
            try
            {
                // Get all files and dirs
                hnd = Win32Native.FindFirstFile(searchPath, data);

                if (hnd.IsInvalid)
                {
                    int hr = Marshal.GetLastWin32Error();

                    // This could happen if the dir doesn't contain any files.
                    // Continue with the recursive search though, eventually
                    // searchStack will become empty
                    if (hr == Win32Native.ERROR_FILE_NOT_FOUND || hr == Win32Native.ERROR_NO_MORE_FILES || hr == Win32Native.ERROR_PATH_NOT_FOUND)
                    {
                        return;
                    }

                    HandleError(hr, localSearchData.fullPath);
                }

                // Add subdirs to searchStack. Exempt ReparsePoints as appropriate
                int incr = 0;
                do
                {
                    if (FileSystemEnumerableHelpers.IsDir(data))
                    {
                        String tempFullPath = Path.InternalCombine(localSearchData.fullPath, data.cFileName);
                        String tempUserPath = Path.InternalCombine(localSearchData.userPath, data.cFileName);

                        SearchOption option = localSearchData.searchOption;

#if EXCLUDE_REPARSEPOINTS
                        // Traverse reparse points depending on the searchoption specified
                        if ((searchDataSubDir.searchOption == SearchOption.AllDirectories) && (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_REPARSE_POINT)))
                        {
                            option = SearchOption.TopDirectoryOnly;
                        }
#endif
                        // Setup search data for the sub directory and push it into the stack
                        Directory.SearchData searchDataSubDir = new Directory.SearchData(tempFullPath, tempUserPath, option);

                        searchStack.Insert(incr++, searchDataSubDir);
                    }
                } while (Win32Native.FindNextFile(hnd, data));
                // We don't care about errors here
            }
            finally
            {
                if (hnd != null)
                {
                    hnd.Dispose();
                }
            }
        }