/// <summary>
        /// Retrieves the list of all directories in the given ftp directory 
        /// </summary>
        /// <param name="path">The remote ftp directory path.</param>        
        /// <returns>Returns the list of diretories present in the given ftp directory.</returns>
        public FtpDirectoryInfo[] GetDirectories(string path)
        {
            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();
            List<FtpDirectoryInfo> directories = new List<FtpDirectoryInfo>();
            IntPtr fileHandle = NativeMethods.FtpFindFirstFile(this.connectionHandle, path, ref findData, NativeMethods.InternetFlagNoCacheWrite, IntPtr.Zero);
            try
            {
                if (fileHandle == IntPtr.Zero)
                {
                    if (Marshal.GetLastWin32Error() == NativeMethods.ErrorNoMoreFiles)
                    {
                        return directories.ToArray();
                    }
                    else
                    {
                        Error();
                        return directories.ToArray();
                    }
                }

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

                findData = new NativeMethods.WIN32_FIND_DATA();

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

                    findData = new NativeMethods.WIN32_FIND_DATA();
                }

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

                return directories.ToArray();
            }
            finally
            {
                if (fileHandle != IntPtr.Zero)
                {
                    int result = NativeMethods.InternetCloseHandle(fileHandle);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieves the list of all directories in the given ftp directory
        /// </summary>
        /// <param name="path">The remote ftp directory path.</param>
        /// <returns>Returns the list of diretories present in the given ftp directory.</returns>
        public FtpDirectoryInfo[] GetDirectories(string path)
        {
            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();
            List <FtpDirectoryInfo>       directories = new List <FtpDirectoryInfo>();
            IntPtr fileHandle = NativeMethods.FtpFindFirstFile(this.connectionHandle, path, ref findData, NativeMethods.InternetFlagNoCacheWrite, IntPtr.Zero);

            try
            {
                if (fileHandle == IntPtr.Zero)
                {
                    if (Marshal.GetLastWin32Error() == NativeMethods.ErrorNoMoreFiles)
                    {
                        return(directories.ToArray());
                    }
                    else
                    {
                        Error();
                        return(directories.ToArray());
                    }
                }

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

                findData = new NativeMethods.WIN32_FIND_DATA();

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

                    findData = new NativeMethods.WIN32_FIND_DATA();
                }

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

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