private FileSystemEnumerableIterator(string fullPath, string normalizedSearchPath, string searchCriteria, string userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost)
 {
     this.fullPath             = fullPath;
     this.normalizedSearchPath = normalizedSearchPath;
     this.searchCriteria       = searchCriteria;
     this._resultHandler       = resultHandler;
     this.userPath             = userPath;
     this.searchOption         = searchOption;
     this._checkHost           = checkHost;
     this.searchStack          = new List <Directory.SearchData>();
     if (searchCriteria != null)
     {
         if (CodeAccessSecurityEngine.QuickCheckForAllDemands())
         {
             FileIOPermission.EmulateFileIOPermissionChecks(fullPath);
             FileIOPermission.EmulateFileIOPermissionChecks(normalizedSearchPath);
         }
         else
         {
             new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[]
             {
                 Directory.GetDemandDir(fullPath, true),
                 Directory.GetDemandDir(normalizedSearchPath, true)
             }, false, false).Demand();
         }
         this.searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption);
         this.CommonInit();
         return;
     }
     this.empty = true;
 }
Exemple #2
0
 internal void DoDemand(string fullPathToDemand)
 {
     new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[1]
     {
         Directory.GetDemandDir(fullPathToDemand, true)
     }, false, false).Demand();
 }
        public DirectoryInfo(String path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            // Special case "<DriveLetter>:" to point to "<CurrentDirectory>" instead
            if ((path.Length == 2) && (path[1] == ':'))
            {
                OriginalPath = ".";
            }
            else
            {
                OriginalPath = path;
            }

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

            demandDir = new String[] { Directory.GetDemandDir(fullPath, true) };
            new FileIOPermission(FileIOPermissionAccess.Read, demandDir, false, false).Demand();

            FullPath = fullPath;
        }
Exemple #4
0
        internal FileSystemEnumerableIterator(string path, string originalUserPath, string searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler)
        {
            this.oldMode     = Win32Native.SetErrorMode(1);
            this.searchStack = new List <Directory.SearchData>();
            string str = FileSystemEnumerableIterator <TSource> .NormalizeSearchPattern(searchPattern);

            if (str.Length == 0)
            {
                this.empty = true;
            }
            else
            {
                this._resultHandler = resultHandler;
                this.searchOption   = searchOption;
                this.fullPath       = Path.GetFullPathInternal(path);
                string fullSearchString = FileSystemEnumerableIterator <TSource> .GetFullSearchString(this.fullPath, str);

                this.normalizedSearchPath = Path.GetDirectoryName(fullSearchString);
                string[] pathList = new string[] { Directory.GetDemandDir(this.fullPath, true), Directory.GetDemandDir(this.normalizedSearchPath, true) };
                new FileIOPermission(FileIOPermissionAccess.PathDiscovery, pathList, false, false).Demand();
                this.searchCriteria = FileSystemEnumerableIterator <TSource> .GetNormalizedSearchCriteria(fullSearchString, this.normalizedSearchPath);

                string directoryName = Path.GetDirectoryName(str);
                string str4          = originalUserPath;
                if ((directoryName != null) && (directoryName.Length != 0))
                {
                    str4 = Path.Combine(str4, directoryName);
                }
                this.userPath   = str4;
                this.searchData = new Directory.SearchData(this.normalizedSearchPath, this.userPath, searchOption);
                this.CommonInit();
            }
        }
        private void Init(String path, bool checkHost)
        {
            // Special case "<DriveLetter>:" to point to "<CurrentDirectory>" instead
            if ((path.Length == 2) && (path[1] == ':'))
            {
                OriginalPath = ".";
            }
            else
            {
                OriginalPath = path;
            }

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

            demandDir = new String[] { Directory.GetDemandDir(fullPath, true) };
#if FEATURE_CORECLR
            if (checkHost)
            {
                FileSecurityState state = new FileSecurityState(FileSecurityStateAccess.Read, OriginalPath, fullPath);
                state.EnsureState();
            }
#else
            new FileIOPermission(FileIOPermissionAccess.Read, demandDir, false, false).Demand();
#endif

            FullPath    = fullPath;
            DisplayPath = GetDisplayName(OriginalPath, FullPath);
        }
 internal DirectoryInfo(string fullPath, bool junk)
 {
     base.OriginalPath = Path.GetFileName(fullPath);
     base.FullPath     = fullPath;
     base.DisplayPath  = GetDisplayName(base.OriginalPath, base.FullPath);
     this.demandDir    = new string[] { Directory.GetDemandDir(fullPath, true) };
 }
        [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));
        }
        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));
        }
 internal DirectoryInfo(String fullPath, bool junk)
 {
     BCLDebug.Assert(Path.GetRootLength(fullPath) > 0, "fullPath must be fully qualified!");
     // Fast path when we know a DirectoryInfo exists.
     OriginalPath = Path.GetFileName(fullPath);
     FullPath     = fullPath;
     demandDir    = new String[] { Directory.GetDemandDir(fullPath, true) };
 }
        [System.Security.SecurityCritical]  // auto-generated
        private DirectoryInfo(SerializationInfo info, StreamingContext context) : base(info, context)
        {
#if !FEATURE_CORECLR
            demandDir = new String[] { Directory.GetDemandDir(FullPath, true) };
            new FileIOPermission(FileIOPermissionAccess.Read, demandDir, false, false).Demand();
#endif
            DisplayPath = GetDisplayName(OriginalPath, FullPath);
        }
Exemple #11
0
 internal void DoDemand(String fullPathToDemand)
 {
     if (_checkHost)
     {
         String            demandDir = Directory.GetDemandDir(fullPathToDemand, true);
         FileSecurityState state     = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandDir);
         state.EnsureState();
     }
 }
        internal void DoDemand(String fullPathToDemand)
        {
#if FEATURE_CORECLR
            if (_checkHost)
            {
                String            demandDir = Directory.GetDemandDir(fullPathToDemand, true);
                FileSecurityState state     = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandDir);
                state.EnsureState();
            }
#else
            String demandDir = Directory.GetDemandDir(fullPathToDemand, true);
            FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, demandDir, false, false);
#endif
        }
        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));
        }
        internal void DoDemand(String fullPathToDemand)
        {
#if FEATURE_CORECLR && !FEATURE_LEGACYNETCFIOSECURITY
            if (_checkHost)
            {
                String            demandDir = Directory.GetDemandDir(fullPathToDemand, true);
                FileSecurityState state     = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandDir);
                state.EnsureState();
            }
#elif !FEATURE_CORECLR
            String   demandDir   = Directory.GetDemandDir(fullPathToDemand, true);
            String[] demandPaths = new String[] { demandDir };
            new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand();
#endif
        }
        internal FileSystemEnumerableIterator(string path, string originalUserPath, string searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost)
        {
            this.oldMode     = Win32Native.SetErrorMode(1);
            this.searchStack = new List <Directory.SearchData>();
            string text = FileSystemEnumerableIterator <TSource> .NormalizeSearchPattern(searchPattern);

            if (text.Length == 0)
            {
                this.empty = true;
                return;
            }
            this._resultHandler = resultHandler;
            this.searchOption   = searchOption;
            this.fullPath       = Path.GetFullPathInternal(path);
            string fullSearchString = FileSystemEnumerableIterator <TSource> .GetFullSearchString(this.fullPath, text);

            this.normalizedSearchPath = Path.GetDirectoryName(fullSearchString);
            if (CodeAccessSecurityEngine.QuickCheckForAllDemands())
            {
                FileIOPermission.EmulateFileIOPermissionChecks(this.fullPath);
                FileIOPermission.EmulateFileIOPermissionChecks(this.normalizedSearchPath);
            }
            else
            {
                new FileIOPermission(FileIOPermissionAccess.PathDiscovery, new string[]
                {
                    Directory.GetDemandDir(this.fullPath, true),
                    Directory.GetDemandDir(this.normalizedSearchPath, true)
                }, false, false).Demand();
            }
            this._checkHost     = checkHost;
            this.searchCriteria = FileSystemEnumerableIterator <TSource> .GetNormalizedSearchCriteria(fullSearchString, this.normalizedSearchPath);

            string directoryName = Path.GetDirectoryName(text);
            string path2         = originalUserPath;

            if (directoryName != null && directoryName.Length != 0)
            {
                path2 = Path.CombineNoChecks(path2, directoryName);
            }
            this.userPath   = path2;
            this.searchData = new Directory.SearchData(this.normalizedSearchPath, this.userPath, searchOption);
            this.CommonInit();
        }
Exemple #16
0
        private FileSystemEnumerableIterator(String fullPath, String normalizedSearchPath, String searchCriteria, String userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost)
        {
            this.fullPath             = fullPath;
            this.normalizedSearchPath = normalizedSearchPath;
            this.searchCriteria       = searchCriteria;
            this._resultHandler       = resultHandler;
            this.userPath             = userPath;
            this.searchOption         = searchOption;
            this._checkHost           = checkHost;

            searchStack = new List <Directory.SearchData>();

            if (searchCriteria != null)
            {
                if (CodeAccessSecurityEngine.QuickCheckForAllDemands())
                {
                    // Full trust, just need to validate incoming paths
                    // (we don't need to get the demand directory as it has no impact)
                    FileIOPermission.EmulateFileIOPermissionChecks(fullPath);
                    FileIOPermission.EmulateFileIOPermissionChecks(normalizedSearchPath);
                }
                else
                {
                    // Not full trust, need to check for rights
                    string[] demandPaths = new string[2];

                    // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters
                    demandPaths[0] = Directory.GetDemandDir(fullPath, true);

                    // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
                    // Do a demand on the combined path so that we can fail early in case of deny
                    demandPaths[1] = Directory.GetDemandDir(normalizedSearchPath, true);

                    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand();
                }
                searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption);
                CommonInit();
            }
            else
            {
                empty = true;
            }
        }
Exemple #17
0
        private FileSystemEnumerableIterator(String fullPath, String normalizedSearchPath, String searchCriteria, String userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost)
        {
            this.fullPath             = fullPath;
            this.normalizedSearchPath = normalizedSearchPath;
            this.searchCriteria       = searchCriteria;
            this._resultHandler       = resultHandler;
            this.userPath             = userPath;
            this.searchOption         = searchOption;
            this._checkHost           = checkHost;

            searchStack = new List <Directory.SearchData>();

            if (searchCriteria != null)
            {
                // permission demands
                String[] demandPaths = new String[2];
                // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters
                demandPaths[0] = Directory.GetDemandDir(fullPath, true);
                // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
                // Do a demand on the combined path so that we can fail early in case of deny
                demandPaths[1] = Directory.GetDemandDir(normalizedSearchPath, true);
#if MONO_FEATURE_CAS
#if FEATURE_CORECLR
                if (checkHost)
                {
                    FileSecurityState state1 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[0]);
                    state1.EnsureState();
                    FileSecurityState state2 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[1]);
                    state2.EnsureState();
                }
#else
                FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false);
#endif
#endif
                searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption);
                CommonInit();
            }
            else
            {
                empty = true;
            }
        }
Exemple #18
0
 private FileSystemEnumerableIterator(string fullPath, string normalizedSearchPath, string searchCriteria, string userPath, SearchOption searchOption, SearchResultHandler <TSource> resultHandler)
 {
     this.fullPath             = fullPath;
     this.normalizedSearchPath = normalizedSearchPath;
     this.searchCriteria       = searchCriteria;
     this._resultHandler       = resultHandler;
     this.userPath             = userPath;
     this.searchOption         = searchOption;
     this.searchStack          = new List <Directory.SearchData>();
     if (searchCriteria != null)
     {
         string[] pathList = new string[] { Directory.GetDemandDir(fullPath, true), Directory.GetDemandDir(normalizedSearchPath, true) };
         new FileIOPermission(FileIOPermissionAccess.PathDiscovery, pathList, false, false).Demand();
         this.searchData = new Directory.SearchData(normalizedSearchPath, userPath, searchOption);
         this.CommonInit();
     }
     else
     {
         this.empty = true;
     }
 }
        public DirectoryInfo(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if ((path.Length == 2) && (path[1] == ':'))
            {
                base.OriginalPath = ".";
            }
            else
            {
                base.OriginalPath = path;
            }
            string fullPathInternal = Path.GetFullPathInternal(path);

            this.demandDir = new string[] { Directory.GetDemandDir(fullPathInternal, true) };
            new FileIOPermission(FileIOPermissionAccess.Read, this.demandDir, false, false).Demand();
            base.FullPath    = fullPathInternal;
            base.DisplayPath = GetDisplayName(base.OriginalPath, base.FullPath);
        }
 private DirectoryInfo(SerializationInfo info, StreamingContext context) : base(info, context)
 {
     this.demandDir = new string[] { Directory.GetDemandDir(base.FullPath, true) };
     new FileIOPermission(FileIOPermissionAccess.Read, this.demandDir, false, false).Demand();
     base.DisplayPath = GetDisplayName(base.OriginalPath, base.FullPath);
 }
 private DirectoryInfo(SerializationInfo info, StreamingContext context) : base(info, context)
 {
     demandDir = new String[] { Directory.GetDemandDir(FullPath, true) };
     new FileIOPermission(FileIOPermissionAccess.Read, demandDir, false, false).Demand();
 }
        public void MoveTo(String destDirName)
        {
            if (destDirName == null)
            {
                throw new ArgumentNullException("destDirName");
            }
            if (destDirName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destDirName");
            }

            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, demandDir, false, false).Demand();
            String fullDestDirName = Path.GetFullPathInternal(destDirName);
            String demandPath;

            if (!fullDestDirName.EndsWith(Path.DirectorySeparatorChar))
            {
                fullDestDirName = fullDestDirName + Path.DirectorySeparatorChar;
            }

            demandPath = fullDestDirName + '.';

            // Demand read & write permission to destination.  The reason is
            // we hand back a DirectoryInfo to the destination that would allow
            // you to read a directory listing from that directory.  Sure, you
            // had the ability to read the file contents in the old location,
            // but you technically also need read permissions to the new
            // location as well, and write is not a true superset of read.
            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, demandPath).Demand();

            String fullSourcePath;

            if (FullPath.EndsWith(Path.DirectorySeparatorChar))
            {
                fullSourcePath = FullPath;
            }
            else
            {
                fullSourcePath = FullPath + Path.DirectorySeparatorChar;
            }

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(fullSourcePath, fullDestDirName, CompareOptions.IgnoreCase) == 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
            }

            String sourceRoot      = Path.GetPathRoot(fullSourcePath);
            String destinationRoot = Path.GetPathRoot(fullDestDirName);

            if (CultureInfo.InvariantCulture.CompareInfo.Compare(sourceRoot, destinationRoot, CompareOptions.IgnoreCase) != 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
            }

            if (Environment.IsWin9X() && !Directory.InternalExists(FullPath)) // For Win9x
            {
                throw new DirectoryNotFoundException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("IO.PathNotFound_Path"), destDirName));
            }


            if (!Win32Native.MoveFile(FullPath, destDirName))
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // A dubious error code
                {
                    hr = Win32Native.ERROR_PATH_NOT_FOUND;
                    __Error.WinIOError(hr, OriginalPath);
                }

                if (hr == Win32Native.ERROR_ACCESS_DENIED) // We did this for Win9x. We can't change it for backcomp.
                {
                    throw new IOException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), OriginalPath));
                }

                __Error.WinIOError(hr, String.Empty);
            }
            FullPath     = fullDestDirName;
            OriginalPath = destDirName;
            demandDir    = new String[] { Directory.GetDemandDir(FullPath, true) };

            // Flush any cached information about the directory.
            _dataInitialised = -1;
        }
Exemple #23
0
 internal static void DoDemand(string fullPath)
 {
     string[] pathList = new string[] { Directory.GetDemandDir(fullPath, true) };
     new FileIOPermission(FileIOPermissionAccess.PathDiscovery, pathList, false, false).Demand();
 }
Exemple #24
0
        internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost)
        {
            Contract.Requires(path != null);
            Contract.Requires(originalUserPath != null);
            Contract.Requires(searchPattern != null);
            Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly);
            Contract.Requires(resultHandler != null);

            oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS);

            searchStack = new List <Directory.SearchData>();

            String normalizedSearchPattern = NormalizeSearchPattern(searchPattern);

            if (normalizedSearchPattern.Length == 0)
            {
                empty = true;
            }
            else
            {
                _resultHandler    = resultHandler;
                this.searchOption = searchOption;

                fullPath = Path.GetFullPathInternal(path);
                String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern);
                normalizedSearchPath = Path.GetDirectoryName(fullSearchString);

                if (CodeAccessSecurityEngine.QuickCheckForAllDemands())
                {
                    // Full trust, just need to validate incoming paths
                    // (we don't need to get the demand directory as it has no impact)
                    FileIOPermission.EmulateFileIOPermissionChecks(fullPath);
                    FileIOPermission.EmulateFileIOPermissionChecks(normalizedSearchPath);
                }
                else
                {
                    // Not full trust, need to check for rights
                    string[] demandPaths = new string[2];

                    // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters
                    demandPaths[0] = Directory.GetDemandDir(fullPath, true);

                    // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
                    // Do a demand on the combined path so that we can fail early in case of deny
                    demandPaths[1] = Directory.GetDemandDir(normalizedSearchPath, true);
                    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand();
                }

                _checkHost = checkHost;

                // normalize search criteria
                searchCriteria = GetNormalizedSearchCriteria(fullSearchString, normalizedSearchPath);

                // fix up user path
                String searchPatternDirName = Path.GetDirectoryName(normalizedSearchPattern);
                String userPathTemp         = originalUserPath;
                if (searchPatternDirName != null && searchPatternDirName.Length != 0)
                {
                    userPathTemp = Path.CombineNoChecks(userPathTemp, searchPatternDirName);
                }
                this.userPath = userPathTemp;

                searchData = new Directory.SearchData(normalizedSearchPath, this.userPath, searchOption);

                CommonInit();
            }
        }
        internal FileSystemEnumerableIterator(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler <TSource> resultHandler, bool checkHost)
        {
            Contract.Requires(path != null);
            Contract.Requires(originalUserPath != null);
            Contract.Requires(searchPattern != null);
            Contract.Requires(searchOption == SearchOption.AllDirectories || searchOption == SearchOption.TopDirectoryOnly);
            Contract.Requires(resultHandler != null);

            oldMode = Win32Native.SetErrorMode(Win32Native.SEM_FAILCRITICALERRORS);

            searchStack = new List <Directory.SearchData>();

            String normalizedSearchPattern = NormalizeSearchPattern(searchPattern);

            if (normalizedSearchPattern.Length == 0)
            {
                empty = true;
            }
            else
            {
                _resultHandler    = resultHandler;
                this.searchOption = searchOption;

                fullPath = Path.GetFullPathInternal(path);
                String fullSearchString = GetFullSearchString(fullPath, normalizedSearchPattern);
                normalizedSearchPath = Path.GetDirectoryName(fullSearchString);

                // permission demands
                String[] demandPaths = new String[2];
                // Any illegal chars such as *, ? will be caught by FileIOPermission.HasIllegalCharacters
                demandPaths[0] = Directory.GetDemandDir(fullPath, true);
                // For filters like foo\*.cs we need to verify if the directory foo is not denied access.
                // Do a demand on the combined path so that we can fail early in case of deny
                demandPaths[1] = Directory.GetDemandDir(normalizedSearchPath, true);
                _checkHost     = checkHost;
#if FEATURE_CORECLR
                if (checkHost)
                {
                    FileSecurityState state1 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[0]);
                    state1.EnsureState();
                    FileSecurityState state2 = new FileSecurityState(FileSecurityStateAccess.PathDiscovery, String.Empty, demandPaths[1]);
                    state2.EnsureState();
                }
#else
                new FileIOPermission(FileIOPermissionAccess.PathDiscovery, demandPaths, false, false).Demand();
#endif

                // normalize search criteria
                searchCriteria = GetNormalizedSearchCriteria(fullSearchString, normalizedSearchPath);

                // fix up user path
                String searchPatternDirName = Path.GetDirectoryName(normalizedSearchPattern);
                String userPathTemp         = originalUserPath;
                if (searchPatternDirName != null && searchPatternDirName.Length != 0)
                {
                    userPathTemp = Path.Combine(userPathTemp, searchPatternDirName);
                }
                this.userPath = userPathTemp;

                searchData = new Directory.SearchData(normalizedSearchPath, this.userPath, searchOption);

                CommonInit();
            }
        }
        internal void DoDemand(string fullPathToDemand)
        {
            string demandDir = Directory.GetDemandDir(fullPathToDemand, true);

            FileIOPermission.QuickDemand(FileIOPermissionAccess.PathDiscovery, demandDir, false, false);
        }
        public void MoveTo(string destDirName)
        {
            string fullPath;

            if (destDirName == null)
            {
                throw new ArgumentNullException("destDirName");
            }
            if (destDirName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destDirName");
            }
            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, this.demandDir, false, false).Demand();
            string fullPathInternal = Path.GetFullPathInternal(destDirName);

            if (!fullPathInternal.EndsWith(Path.DirectorySeparatorChar))
            {
                fullPathInternal = fullPathInternal + Path.DirectorySeparatorChar;
            }
            string path = fullPathInternal + '.';

            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, path).Demand();
            if (base.FullPath.EndsWith(Path.DirectorySeparatorChar))
            {
                fullPath = base.FullPath;
            }
            else
            {
                fullPath = base.FullPath + Path.DirectorySeparatorChar;
            }
            if (string.Compare(fullPath, fullPathInternal, StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
            }
            string pathRoot = Path.GetPathRoot(fullPath);
            string strB     = Path.GetPathRoot(fullPathInternal);

            if (string.Compare(pathRoot, strB, StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
            }
            if (!Win32Native.MoveFile(base.FullPath, destDirName))
            {
                int errorCode = Marshal.GetLastWin32Error();
                switch (errorCode)
                {
                case 2:
                    errorCode = 3;
                    __Error.WinIOError(errorCode, base.DisplayPath);
                    break;

                case 5:
                    throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", new object[] { base.DisplayPath }));
                }
                __Error.WinIOError(errorCode, string.Empty);
            }
            base.FullPath         = fullPathInternal;
            base.OriginalPath     = destDirName;
            base.DisplayPath      = GetDisplayName(base.OriginalPath, base.FullPath);
            this.demandDir        = new string[] { Directory.GetDemandDir(base.FullPath, true) };
            base._dataInitialised = -1;
        }
        public void MoveTo(String destDirName)
        {
            if (destDirName == null)
            {
                throw new ArgumentNullException(nameof(destDirName));
            }
            if (destDirName.Length == 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), nameof(destDirName));
            }
            Contract.EndContractBlock();

#if FEATURE_CORECLR
            FileSecurityState sourceState = new FileSecurityState(FileSecurityStateAccess.Write | FileSecurityStateAccess.Read, DisplayPath, Directory.GetDemandDir(FullPath, true));
            sourceState.EnsureState();
#else
            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, demandDir, false, false).Demand();
#endif
            String fullDestDirName = Path.GetFullPathInternal(destDirName);
            String demandPath;
            if (!fullDestDirName.EndsWith(Path.DirectorySeparatorChar))
            {
                fullDestDirName = fullDestDirName + Path.DirectorySeparatorChar;
            }

            demandPath = fullDestDirName + '.';

            // Demand read & write permission to destination.  The reason is
            // we hand back a DirectoryInfo to the destination that would allow
            // you to read a directory listing from that directory.  Sure, you
            // had the ability to read the file contents in the old location,
            // but you technically also need read permissions to the new
            // location as well, and write is not a true superset of read.
#if FEATURE_CORECLR
            FileSecurityState destState = new FileSecurityState(FileSecurityStateAccess.Write, destDirName, demandPath);
            destState.EnsureState();
#else
            new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, demandPath).Demand();
#endif

            String fullSourcePath;
            if (FullPath.EndsWith(Path.DirectorySeparatorChar))
            {
                fullSourcePath = FullPath;
            }
            else
            {
                fullSourcePath = FullPath + Path.DirectorySeparatorChar;
            }

            if (String.Compare(fullSourcePath, fullDestDirName, StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustBeDifferent"));
            }

            String sourceRoot      = Path.GetPathRoot(fullSourcePath);
            String destinationRoot = Path.GetPathRoot(fullDestDirName);

            if (String.Compare(sourceRoot, destinationRoot, StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new IOException(Environment.GetResourceString("IO.IO_SourceDestMustHaveSameRoot"));
            }

            if (!Win32Native.MoveFile(FullPath, destDirName))
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // A dubious error code
                {
                    hr = Win32Native.ERROR_PATH_NOT_FOUND;
                    __Error.WinIOError(hr, DisplayPath);
                }

                if (hr == Win32Native.ERROR_ACCESS_DENIED) // We did this for Win9x. We can't change it for backcomp.
                {
                    throw new IOException(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path", DisplayPath));
                }

                __Error.WinIOError(hr, String.Empty);
            }
            FullPath     = fullDestDirName;
            OriginalPath = destDirName;
            DisplayPath  = GetDisplayName(OriginalPath, FullPath);
            demandDir    = new String[] { Directory.GetDemandDir(FullPath, true) };

            // Flush any cached information about the directory.
            _dataInitialised = -1;
        }