Exemple #1
0
        /// <summary>
        /// Checks whether the given file exists on the remote ftp server or not
        /// </summary>
        /// <param name="path">The path of the file to check whether it exists or not.</param>
        /// <returns>True if the file exists, false otherwise</returns>
        public bool FileExists(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();
            IntPtr fileHandle = NativeMethods.FtpFindFirstFile(this.connectionHandle, path, ref findData, NativeMethods.InternetFlagNoCacheWrite, IntPtr.Zero);

            return(fileHandle != IntPtr.Zero);
        }
Exemple #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();

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

            try
            {
                List <FtpDirectoryInfo> directories = new List <FtpDirectoryInfo>();

                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)
                {
                    NativeMethods.InternetCloseHandle(fileHandle);
                }
            }
        }
        /// <summary>
        /// Checks whether the given file exists on the remote ftp server or not
        /// </summary>
        /// <param name="path">The path of the file to check whether it exists or not.</param>
        /// <returns>True if the file exists, false otherwise</returns>
        public bool FileExists(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();
            IntPtr fileHandle = NativeMethods.FtpFindFirstFile(this.connectionHandle, path, ref findData, NativeMethods.InternetFlagNoCacheWrite, IntPtr.Zero);
            return fileHandle != IntPtr.Zero;            
        }
        /// <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();

            IntPtr fileHandle = NativeMethods.FtpFindFirstFile(this.connectionHandle, path, ref findData, NativeMethods.InternetFlagNoCacheWrite, IntPtr.Zero);
            try
            {
                List<FtpDirectoryInfo> directories = new List<FtpDirectoryInfo>();

                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)
                {
                    NativeMethods.InternetCloseHandle(fileHandle);
                }
            }
        }
 public static extern int InternetFindNextFile(
     [In] IntPtr hInternet,
     [In][Out] ref NativeMethods.WIN32_FIND_DATA findData);
 public static extern IntPtr FtpFindFirstFile(
     [In] IntPtr hConnect,
     [In] string searchFile,
     [In][Out] ref NativeMethods.WIN32_FIND_DATA findFileData,
     [In] int dwFlags,
     [In] IntPtr dwContext);