internal override FileSystemInfo CreateObject(SearchResult result)
        {
            FileSystemEnumerableHelpers.IsFile(result.FindData);
            if (FileSystemEnumerableHelpers.IsDir(result.FindData))
            {
                string fullPath = result.FullPath;
                new FileIOPermission(FileIOPermissionAccess.Read, new string[1]
                {
                    fullPath + "\\."
                }, false, false).Demand();
                DirectoryInfo directoryInfo          = new DirectoryInfo(fullPath, false);
                Win32Native.WIN32_FIND_DATA findData = result.FindData;
                directoryInfo.InitializeFrom(findData);
                return((FileSystemInfo)directoryInfo);
            }
            string fullPath1 = result.FullPath;

            new FileIOPermission(FileIOPermissionAccess.Read, new string[1] {
                fullPath1
            }, false, false).Demand();
            FileInfo fileInfo = new FileInfo(fullPath1, false);

            Win32Native.WIN32_FIND_DATA findData1 = result.FindData;
            fileInfo.InitializeFrom(findData1);
            return((FileSystemInfo)fileInfo);
        }
Example #2
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);
            }
        }
Example #3
0
        private static List <string> Search(string path, bool isFiles)
        {
            var fullPath   = Path.GetFullPath(path);
            var searchPath = Path.Combine(fullPath, "*.*");
            var rv         = new List <string>();
            var findData   = new Win32Native.WIN32_FIND_DATA();

            using (var hndFindFile = Win32Native.FindFirstFile(searchPath, findData))
            {
                if (hndFindFile == null)
                {
                    return(rv);
                }
                var retval = true;
                while (retval)
                {
                    if ((findData.cFileName == ".") || (findData.cFileName == ".."))
                    {
                        retval = Win32Native.FindNextFile(hndFindFile, findData);
                        continue;
                    }
                    if (((findData.dwFileAttributes & FileAttributes.Directory) == FileAttributes.Directory) != isFiles)
                    {
                        rv.Add(Path.Combine(fullPath, findData.cFileName));
                    }
                    retval = Win32Native.FindNextFile(hndFindFile, findData);
                }
            }
            return(rv);
        }
 internal static bool IsDir(Win32Native.WIN32_FIND_DATA data)
 {
     if ((data.dwFileAttributes & 16) != 0 && !data.cFileName.Equals("."))
     {
         return(!data.cFileName.Equals(".."));
     }
     return(false);
 }
Example #5
0
        internal SearchResult(String fullPath, String userPath, Win32Native.WIN32_FIND_DATA findData)
        {
            Debug.Assert(fullPath != null);
            Debug.Assert(userPath != null);

            this.fullPath = fullPath;
            this.userPath = userPath;
            this.findData = findData;
        }
        internal SearchResult(String fullPath, String userPath, Win32Native.WIN32_FIND_DATA findData)
        {
            Contract.Requires(fullPath != null);
            Contract.Requires(userPath != null);

            this.fullPath = fullPath;
            this.userPath = userPath;
            this.findData = findData;
        }
Example #7
0
        internal void InitializeFrom(Win32Native.WIN32_FIND_DATA findData)
        {
#if MONO
            throw new NotImplementedException();
#else
            _data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
            _data.PopulateFrom(findData);
            _dataInitialised = 0;
#endif
        }
Example #8
0
        internal override FileInfo CreateObject(SearchResult result)
        {
            string fullPath = result.FullPath;

            new FileIOPermission(FileIOPermissionAccess.Read, new string[1] {
                fullPath
            }, false, false).Demand();
            FileInfo fileInfo = new FileInfo(fullPath, false);

            Win32Native.WIN32_FIND_DATA findData = result.FindData;
            fileInfo.InitializeFrom(findData);
            return(fileInfo);
        }
Example #9
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();
                }
            }
        }
        internal static FileInfo CreateFileInfo(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData)
        {
            string cFileName = findData.cFileName;
            string text      = Path.CombineNoChecks(searchData.fullPath, cFileName);

            if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
            {
                new FileIOPermission(FileIOPermissionAccess.Read, new string[]
                {
                    text
                }, false, false).Demand();
            }
            FileInfo fileInfo = new FileInfo(text, cFileName);

            fileInfo.InitializeFrom(ref findData);
            return(fileInfo);
        }
Example #11
0
        internal static FileInfo CreateFileInfo(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData)
        {
            string fileName = findData.cFileName;
            string fullPath = Path.CombineNoChecks(searchData.fullPath, fileName);

            if (!CodeAccessSecurityEngine.QuickCheckForAllDemands())
            {
                // There is no need to emulate checks that FileIOPermission does if we aren't in full trust.
                // The paths we're getting are already tested and/or coming straight from the OS.
                new FileIOPermission(FileIOPermissionAccess.Read, new string[] { fullPath }, false, false).Demand();
            }

            FileInfo fi = new FileInfo(fullPath, fileName);

            fi.InitializeFrom(ref findData);
            return(fi);
        }
Example #12
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();
                }
            }
        }
Example #14
0
        private void CommonInit()
        {
            string fileName = this.searchData.fullPath + this.searchCriteria;

            Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();
            this._hnd = Win32Native.FindFirstFile(fileName, data);
            if (this._hnd.IsInvalid)
            {
                int hr = Marshal.GetLastWin32Error();
                if ((hr != 2) && (hr != 0x12))
                {
                    this.HandleError(hr, this.searchData.fullPath);
                }
                else
                {
                    this.empty = this.searchData.searchOption == SearchOption.TopDirectoryOnly;
                }
            }
            if (this.searchData.searchOption == SearchOption.TopDirectoryOnly)
            {
                if (this.empty)
                {
                    this._hnd.Dispose();
                }
                else
                {
                    SearchResult result = this.CreateSearchResult(this.searchData, data);
                    if (this._resultHandler.IsResultIncluded(result))
                    {
                        base.current = this._resultHandler.CreateObject(result);
                    }
                }
            }
            else
            {
                this._hnd.Dispose();
                this.searchStack.Add(this.searchData);
            }
        }
Example #15
0
        private void AddSearchableDirsToStack(Directory.SearchData localSearchData)
        {
            string         fileName    = localSearchData.fullPath + "*";
            SafeFindHandle hndFindFile = null;

            Win32Native.WIN32_FIND_DATA win_find_data = new Win32Native.WIN32_FIND_DATA();
            using (hndFindFile = Win32Native.FindFirstFile(fileName, win_find_data))
            {
                if (hndFindFile.IsInvalid)
                {
                    int hr = Marshal.GetLastWin32Error();
                    switch (hr)
                    {
                    case 2:
                    case 0x12:
                    case 3:
                        return;
                    }
                    this.HandleError(hr, localSearchData.fullPath);
                }
                int num2 = 0;
                do
                {
                    if (FileSystemEnumerableHelpers.IsDir(win_find_data))
                    {
                        StringBuilder builder = new StringBuilder(localSearchData.fullPath);
                        builder.Append(win_find_data.cFileName);
                        string fullPath = builder.ToString();
                        builder.Length = 0;
                        builder.Append(localSearchData.userPath);
                        builder.Append(win_find_data.cFileName);
                        SearchOption         searchOption = localSearchData.searchOption;
                        Directory.SearchData item         = new Directory.SearchData(fullPath, builder.ToString(), searchOption);
                        this.searchStack.Insert(num2++, item);
                    }
                }while (Win32Native.FindNextFile(hndFindFile, win_find_data));
            }
        }
        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();
                }
            }
        }
        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 override bool MoveNext()
        {
            Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();
            switch (state)
            {
            case STATE_INIT:
            {
                if (empty)
                {
                    state = STATE_FINISH;
                    goto case STATE_FINISH;
                }
                if (searchData.searchOption == SearchOption.TopDirectoryOnly)
                {
                    state = STATE_FIND_NEXT_FILE;
                    if (current != null)
                    {
                        return(true);
                    }
                    else
                    {
                        goto case STATE_FIND_NEXT_FILE;
                    }
                }
                else
                {
                    state = STATE_SEARCH_NEXT_DIR;
                    goto case STATE_SEARCH_NEXT_DIR;
                }
            }

            case STATE_SEARCH_NEXT_DIR:
            {
                Contract.Assert(searchData.searchOption != SearchOption.TopDirectoryOnly, "should not reach this code path if searchOption == TopDirectoryOnly");
                // Traverse directory structure. We need to get '*'
                while (searchStack.Count > 0)
                {
                    searchData = searchStack[0];
                    Contract.Assert((searchData.fullPath != null), "fullpath can't be null!");
                    searchStack.RemoveAt(0);

                    // Traverse the subdirs
                    AddSearchableDirsToStack(searchData);

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

                    // Open a Find handle
                    _hnd = Win32Native.FindFirstFile(searchPath, data);
                    if (_hnd.IsInvalid)
                    {
                        int hr = Marshal.GetLastWin32Error();
                        if (hr == Win32Native.ERROR_FILE_NOT_FOUND || hr == Win32Native.ERROR_NO_MORE_FILES || hr == Win32Native.ERROR_PATH_NOT_FOUND)
                        {
                            continue;
                        }

                        _hnd.Dispose();
                        HandleError(hr, searchData.fullPath);
                    }

                    state = STATE_FIND_NEXT_FILE;
                    needsParentPathDiscoveryDemand = true;
                    SearchResult searchResult = CreateSearchResult(searchData, data);
                    if (_resultHandler.IsResultIncluded(searchResult))
                    {
                        if (needsParentPathDiscoveryDemand)
                        {
                            DoDemand(searchData.fullPath);
                            needsParentPathDiscoveryDemand = false;
                        }
                        current = _resultHandler.CreateObject(searchResult);
                        return(true);
                    }
                    else
                    {
                        goto case STATE_FIND_NEXT_FILE;
                    }
                }
                state = STATE_FINISH;
                goto case STATE_FINISH;
            }

            case STATE_FIND_NEXT_FILE:
            {
                if (searchData != null && _hnd != null)
                {
                    // Keep asking for more matching files/dirs, add it to the list
                    while (Win32Native.FindNextFile(_hnd, data))
                    {
                        SearchResult searchResult = CreateSearchResult(searchData, data);
                        if (_resultHandler.IsResultIncluded(searchResult))
                        {
                            if (needsParentPathDiscoveryDemand)
                            {
                                DoDemand(searchData.fullPath);
                                needsParentPathDiscoveryDemand = false;
                            }
                            current = _resultHandler.CreateObject(searchResult);
                            return(true);
                        }
                    }

                    // Make sure we quit with a sensible error.
                    int hr = Marshal.GetLastWin32Error();

                    if (_hnd != null)
                    {
                        _hnd.Dispose();
                    }

                    // ERROR_FILE_NOT_FOUND is valid here because if the top level
                    // dir doen't contain any subdirs and matching files then
                    // we will get here with this errorcode from the searchStack walk
                    if ((hr != 0) && (hr != Win32Native.ERROR_NO_MORE_FILES) &&
                        (hr != Win32Native.ERROR_FILE_NOT_FOUND))
                    {
                        HandleError(hr, searchData.fullPath);
                    }
                }
                if (searchData.searchOption == SearchOption.TopDirectoryOnly)
                {
                    state = STATE_FINISH;
                    goto case STATE_FINISH;
                }
                else
                {
                    state = STATE_SEARCH_NEXT_DIR;
                    goto case STATE_SEARCH_NEXT_DIR;
                }
            }

            case STATE_FINISH:
            {
                Dispose();
                break;
            }
            }
            return(false);
        }
Example #20
0
 internal SearchResult(string fullPath, string userPath, Win32Native.WIN32_FIND_DATA findData)
 {
     this.fullPath = fullPath;
     this.userPath = userPath;
     this.findData = findData;
 }
Example #21
0
        // From IO.Directory class (make that internal if possible)
        private static String[] GetFileDirectoryNames(String path, String msg, bool file)
        {
            int hr;

            if (path == null)
            {
                throw new ArgumentNullException("path", Environment.GetResourceString("ArgumentNull_Path"));
            }

            bool fEndsWithDirectory = false;
            char lastChar           = path[path.Length - 1];

            if (lastChar == Path.DirectorySeparatorChar ||
                lastChar == Path.AltDirectorySeparatorChar ||
                lastChar == '.')
            {
                fEndsWithDirectory = true;
            }


            // Get an absolute path and do a security check
            String fullPath = Path.GetFullPathInternal(path);

            // GetFullPath() removes '\', "\." etc from path, we will restore
            // it here. If path ends in a trailing slash (\), append a *
            // or we'll  get a "Cannot find the file specified" exception
            if ((fEndsWithDirectory) &&
                (fullPath[fullPath.Length - 1] != lastChar))
            {
                fullPath += "\\*";
            }

            // Check for read permission to the directory, not to the contents.
            String dir = Path.GetDirectoryName(fullPath);

            if (dir != null)
            {
                dir += "\\";
            }

#if _DEBUG
            if (s_fDebug)
            {
                Console.WriteLine("path = " + path);
                Console.WriteLine("fullPath = " + fullPath);
                Console.WriteLine("dir = " + dir);
            }
#endif

            new FileIOPermission(FileIOPermissionAccess.Read, dir == null ? fullPath : dir).Demand();


            String[] list     = new String[10];
            int      listSize = 0;
            Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();

            // Open a Find handle (Win32 is weird)
            IntPtr hnd = Win32Native.FindFirstFile(fullPath, data);
            if (hnd == Win32Native.INVALID_HANDLE_VALUE)
            {
                // Calls to GetLastWin32Error clobber HResult.  Store HResult.
                hr = Marshal.GetLastWin32Error();
                if (hr == Win32Native.ERROR_FILE_NOT_FOUND)
                {
                    return(new String[0]);
                }
                __Error.WinIOError(hr, msg);
            }

            // Keep asking for more matching files, adding file names to list
            int numEntries = 0;      // Number of directory entities we see.
            do
            {
                bool includeThis;  // Should this file/directory be included in the output?
                if (file)
                {
                    includeThis = (0 == (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY));
                }
                else
                {
                    includeThis = (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY));
                    // Don't add "." nor ".."
                    if (includeThis && (data.cFileName.Equals(".") || data.cFileName.Equals("..")))
                    {
                        includeThis = false;
                    }
                }

                if (includeThis)
                {
                    numEntries++;
                    if (listSize == list.Length)
                    {
                        String[] newList = new String[list.Length * 2];
                        Array.Copy(list, 0, newList, 0, listSize);
                        list = newList;
                    }
                    list[listSize++] = data.cFileName;
                }
            } while (Win32Native.FindNextFile(hnd, data));

            // Make sure we quit with a sensible error.
            hr = Marshal.GetLastWin32Error();
            Win32Native.FindClose(hnd);      // Close Find handle in all cases.
            if (hr != 0 && hr != Win32Native.ERROR_NO_MORE_FILES)
            {
                __Error.WinIOError(hr, msg);
            }

            // Check for a string such as "C:\tmp", in which case we return
            // just the directory name.  FindNextFile fails first time, and
            // data still contains a directory.
            if (!file && numEntries == 1 && (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY)))
            {
                String[] sa = new String[1];
                sa[0] = data.cFileName;
                return(sa);
            }

            // Return list of files/directories as an array of strings
            if (listSize == list.Length)
            {
                return(list);
            }
            String[] items = new String[listSize];
            Array.Copy(list, 0, items, 0, listSize);
            return(items);
        }
 internal override FileInfo CreateObject(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData)
 {
     return(FileInfoResultHandler.CreateFileInfo(searchData, ref findData));
 }
 internal override bool IsResultIncluded(ref Win32Native.WIN32_FIND_DATA findData)
 {
     return(findData.IsFile);
 }
Example #24
0
        /// <include file='doc\Directory.uex' path='docs/doc[@for="Directory.Delete1"]/*' />
        public static void Delete(String path, bool recursive)
        {
            String fullPath = Path.GetFullPathInternal(path);
            String demandPath;

            if (!recursive)   // Do normal check only on this directory
            {
                if (fullPath.EndsWith(Path.DirectorySeparatorChar))
                {
                    demandPath = fullPath + ".";
                }
                else
                {
                    demandPath = fullPath + Path.DirectorySeparatorChar + ".";
                }
            }
            else   // Check for deny anywhere below and fail
            {
                if (fullPath.EndsWith(Path.DirectorySeparatorChar))
                {
                    demandPath = fullPath.Substring(0, fullPath.Length - 1);
                }
                else
                {
                    demandPath = fullPath;
                }
            }

            // Make sure we have write permission to this directory
            new FileIOPermission(FileIOPermissionAccess.Write, new String[] { demandPath }, false, false).Demand();

            bool      r;
            int       hr;
            Exception ex = null;

            if (recursive)
            {
                Win32Native.WIN32_FIND_DATA data = new Win32Native.WIN32_FIND_DATA();

                // Open a Find handle (Win32 is weird)
                IntPtr hnd = Win32Native.FindFirstFile(fullPath + Path.DirectorySeparatorChar + "*", data);
                if (hnd == Win32Native.INVALID_HANDLE_VALUE)
                {
                    hr = Marshal.GetLastWin32Error();
                    __Error.WinIOError(hr, path);
                }

                do
                {
                    bool isDir = (0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY));
                    if (isDir)
                    {
                        if (data.cFileName.Equals(".") || data.cFileName.Equals(".."))
                        {
                            continue;
                        }

                        // recurse
                        DirectoryInfo d = new DirectoryInfo(Path.InternalCombine(fullPath, data.cFileName), false);
                        try {
                            d.Delete(true);
                        }
                        catch (Exception e) {
                            if (ex == null)
                            {
                                ex = e;
                            }
                        }
                    }
                    else
                    {
                        String fileName = path + Path.DirectorySeparatorChar + data.cFileName;
                        r = Win32Native.DeleteFile(fileName);
                        if (!r)
                        {
                            hr = Marshal.GetLastWin32Error();
                            try {
                                __Error.WinIOError(hr, data.cFileName);
                            }
                            catch (Exception e) {
                                if (ex == null)
                                {
                                    ex = e;
                                }
                            }
                        }
                    }
                } while (Win32Native.FindNextFile(hnd, data));

                // Make sure we quit with a sensible error.
                hr = Marshal.GetLastWin32Error();
                Win32Native.FindClose(hnd);  // Close Find handle in all cases.
                if (ex != null)
                {
                    throw ex;
                }
                if (hr != 0 && hr != Win32Native.ERROR_NO_MORE_FILES)
                {
                    __Error.WinIOError(hr, path);
                }
            }

            r  = Win32Native.RemoveDirectory(fullPath);
            hr = Marshal.GetLastWin32Error();
            if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Win32 is weird, it gives back a file not found
            {
                hr = Win32Native.ERROR_PATH_NOT_FOUND;
            }
            if (hr == Win32Native.ERROR_ACCESS_DENIED) // WinNT throws IOException. Win9x hack to do the same.
            {
                throw new IOException(String.Format(Environment.GetResourceString("UnauthorizedAccess_IODenied_Path"), path));
            }

            if (!r)
            {
                __Error.WinIOError(hr, path);
            }
        }
 internal void InitializeFrom(ref Win32Native.WIN32_FIND_DATA findData)
 {
     this._data = default(Win32Native.WIN32_FILE_ATTRIBUTE_DATA);
     this._data.PopulateFrom(ref findData);
     this._dataInitialised = 0;
 }
 [System.Security.SecurityCritical]  // auto-generated
 internal static bool IsFile(Win32Native.WIN32_FIND_DATA data)
 {
     return(0 == (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY));
 }
 [System.Security.SecurityCritical]  // auto-generated
 internal static bool IsDir(Win32Native.WIN32_FIND_DATA data)
 {
     // Don't add "." nor ".."
     return((0 != (data.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY)) &&
            !data.cFileName.Equals(".") && !data.cFileName.Equals(".."));
 }
 internal abstract bool IsResultIncluded(ref Win32Native.WIN32_FIND_DATA findData);
Example #29
0
 internal void InitializeFrom(Win32Native.WIN32_FIND_DATA findData)
 {
     _data = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA();
     _data.PopulateFrom(findData);
     _dataInitialised = 0;
 }
 internal abstract TSource CreateObject(Directory.SearchData searchData, ref Win32Native.WIN32_FIND_DATA findData);