/// <summary>
        /// Retrieves the list of all files in the ftp directory currently selected whose name matches the fileName mask
        /// </summary>                
        /// <param name="mask">The search criteria to return files.</param>        
        /// <returns>Returns the list of files present in the current ftp directory.</returns>        
        public FtpFileInfo[] GetFiles(string mask)
        {
            if (this.connectionHandle == IntPtr.Zero)
            {
                throw new FtpException("The user is not connected to the FTP server. Please connect and try again.");
            }

            NativeMethods.WIN32_FIND_DATA findData = new NativeMethods.WIN32_FIND_DATA();

            IntPtr fileHandle = NativeMethods.FtpFindFirstFile(this.connectionHandle, mask, ref findData, NativeMethods.InternetFlagNoCacheWrite, IntPtr.Zero);
            try
            {
                List<FtpFileInfo> files = new List<FtpFileInfo>();
                if (fileHandle == IntPtr.Zero)
                {
                    if (Marshal.GetLastWin32Error() == NativeMethods.ErrorNoMoreFiles)
                    {
                        return files.ToArray();
                    }
                    else
                    {
                        Error();
                        return files.ToArray();
                    }
                }

                if ((findData.dfFileAttributes & NativeMethods.FileAttributeDirectory) != NativeMethods.FileAttributeDirectory)
                {
                    FtpFileInfo file = new FtpFileInfo(this, new string(findData.fileName).TrimEnd('\0')) { LastAccessTime = findData.ftLastAccessTime.ToDateTime(), LastWriteTime = findData.ftLastWriteTime.ToDateTime(), CreationTime = findData.ftCreationTime.ToDateTime(), Attributes = (FileAttributes)findData.dfFileAttributes };
                    files.Add(file);
                }

                findData = new NativeMethods.WIN32_FIND_DATA();
                while (NativeMethods.InternetFindNextFile(fileHandle, ref findData) != 0)
                {
                    if ((findData.dfFileAttributes & NativeMethods.FileAttributeDirectory) != NativeMethods.FileAttributeDirectory)
                    {
                        FtpFileInfo file = new FtpFileInfo(this, new string(findData.fileName).TrimEnd('\0')) { LastAccessTime = findData.ftLastAccessTime.ToDateTime(), LastWriteTime = findData.ftLastWriteTime.ToDateTime(), CreationTime = findData.ftCreationTime.ToDateTime(), Attributes = (FileAttributes)findData.dfFileAttributes };
                        files.Add(file);
                    }

                    findData = new NativeMethods.WIN32_FIND_DATA();
                }

                if (Marshal.GetLastWin32Error() != NativeMethods.ErrorNoMoreFiles)
                {
                    Error();
                }

                return files.ToArray();
            }
            finally
            {
                if (fileHandle != IntPtr.Zero)
                {
                    NativeMethods.InternetCloseHandle(fileHandle);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Retrieves the list of all files in the ftp directory currently selected whose name matches the fileName mask
        /// </summary>
        /// <param name="mask">The search criteria to return files.</param>
        /// <returns>Returns the list of files present in the current ftp directory.</returns>
        public FtpFileInfo[] GetFiles(string mask)
        {
            if (this.connectionHandle == IntPtr.Zero)
            {
                throw new FtpException("The user is not connected to the FTP server. Please connect and try again.");
            }

            NativeMethods.WIN32_FIND_DATA findData = new NativeMethods.WIN32_FIND_DATA();

            IntPtr fileHandle = NativeMethods.FtpFindFirstFile(this.connectionHandle, mask, ref findData, NativeMethods.InternetFlagNoCacheWrite, IntPtr.Zero);

            try
            {
                List <FtpFileInfo> files = new List <FtpFileInfo>();
                if (fileHandle == IntPtr.Zero)
                {
                    if (Marshal.GetLastWin32Error() == NativeMethods.ErrorNoMoreFiles)
                    {
                        return(files.ToArray());
                    }
                    else
                    {
                        Error();
                        return(files.ToArray());
                    }
                }

                if ((findData.dfFileAttributes & NativeMethods.FileAttributeDirectory) != NativeMethods.FileAttributeDirectory)
                {
                    FtpFileInfo file = new FtpFileInfo(this, new string(findData.fileName).TrimEnd('\0'))
                    {
                        LastAccessTime = findData.ftLastAccessTime.ToDateTime(), LastWriteTime = findData.ftLastWriteTime.ToDateTime(), CreationTime = findData.ftCreationTime.ToDateTime(), Attributes = (FileAttributes)findData.dfFileAttributes
                    };
                    files.Add(file);
                }

                findData = new NativeMethods.WIN32_FIND_DATA();
                while (NativeMethods.InternetFindNextFile(fileHandle, ref findData) != 0)
                {
                    if ((findData.dfFileAttributes & NativeMethods.FileAttributeDirectory) != NativeMethods.FileAttributeDirectory)
                    {
                        FtpFileInfo file = new FtpFileInfo(this, new string(findData.fileName).TrimEnd('\0'))
                        {
                            LastAccessTime = findData.ftLastAccessTime.ToDateTime(), LastWriteTime = findData.ftLastWriteTime.ToDateTime(), CreationTime = findData.ftCreationTime.ToDateTime(), Attributes = (FileAttributes)findData.dfFileAttributes
                        };
                        files.Add(file);
                    }

                    findData = new NativeMethods.WIN32_FIND_DATA();
                }

                if (Marshal.GetLastWin32Error() != NativeMethods.ErrorNoMoreFiles)
                {
                    Error();
                }

                return(files.ToArray());
            }
            finally
            {
                if (fileHandle != IntPtr.Zero)
                {
                    NativeMethods.InternetCloseHandle(fileHandle);
                }
            }
        }