Exemplo n.º 1
0
 public void RemoveFile(string fileName)
 {
     if (WININET.FtpDeleteFile(_hConnect, fileName) == 0)
     {
         Error();
     }
 }
Exemplo n.º 2
0
 public void RenameFile(string existingFile, string newFile)
 {
     if (WININET.FtpRenameFile(_hConnect, existingFile, newFile) == 0)
     {
         Error();
     }
 }
Exemplo n.º 3
0
 public void PutFile(string localFile, string remoteFile)
 {
     if (WININET.FtpPutFile(_hConnect, localFile, remoteFile, 2, IntPtr.Zero) == 0)
     {
         Error();
     }
 }
Exemplo n.º 4
0
 public void RemoveDirectory(string directory)
 {
     if (WININET.FtpRemoveDirectory(_hConnect, directory) == 0)
     {
         Error();
     }
 }
Exemplo n.º 5
0
 public void GetFile(string remoteFile, string localFile, bool failIfExists)
 {
     if (WININET.FtpGetFile(_hConnect, remoteFile, localFile, failIfExists, 0x80, 2, IntPtr.Zero) == 0)
     {
         Error();
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Logs into the host server using the provided credentials.
        /// </summary>
        /// <exception cref="ArgumentNullException">If <paramref name="username"/> or <paramref name="password"/> are null.</exception>
        /// <param name="username">A <see cref="String" /> type representing the user name with which to authenticate.</param>
        /// <param name="password">A <see cref="String" /> type representing the password with which to authenticate.</param>
        public void Login(string username, string password)
        {
            if (username == null)
            {
                throw new ArgumentNullException("username");
            }

            if (password == null)
            {
                throw new ArgumentNullException("password");
            }

            {
            }
            _hConnect = WININET.InternetConnect(_hInternet,
                                                Host,
                                                Port,
                                                username,
                                                password,
                                                WININET.INTERNET_SERVICE_FTP,
                                                WININET.INTERNET_FLAG_PASSIVE,
                                                IntPtr.Zero);

            if (_hConnect == IntPtr.Zero)
            {
                Error();
            }
        }
Exemplo n.º 7
0
        public long GetFileSize(string file)
        {
            IntPtr hConnect = new(WININET.FtpOpenFile(_hConnect, file, 0x8000_0000, 2, IntPtr.Zero));

            switch ((hConnect == IntPtr.Zero))
            {
            case false:
                try
                {
                    int dwFileSizeHigh = 0;
                    int num2           = WININET.FtpGetFileSize(hConnect, ref dwFileSizeHigh);
                    return((dwFileSizeHigh << 0x20) | num2);
                }
                catch (Exception)
                {
                    Error();
                }
                finally
                {
                    WININET.InternetCloseHandle(hConnect);
                }

                break;

            default:
                Error();
                break;
            }

            return(0L);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Gets the specified file's size
        /// </summary>
        /// <param name="file">The file to get the size for</param>
        /// <returns>The file size in bytes</returns>
        public long GetFileSize(string file)
        {
            IntPtr hFile = new IntPtr(
                WININET.FtpOpenFile(_hConnect, file, WINAPI.GENERIC_READ, WININET.FTP_TRANSFER_TYPE_BINARY, IntPtr.Zero)
                );

            if (hFile == IntPtr.Zero)
            {
                Error();
            }
            else
            {
                try
                {
                    int sizeHigh = 0;
                    int sizeLo   = WININET.FtpGetFileSize(hFile, ref sizeHigh);

                    long fileSize = ((long)sizeHigh << 32) | sizeLo;

                    return(fileSize);
                }
                catch (Exception)
                {
                    Error();
                }
                finally
                {
                    WININET.InternetCloseHandle(hFile);
                }
            }

            return(0);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets the specified file's size
        /// </summary>
        /// <param name="file">The file to get the size for</param>
        /// <returns>The file size in bytes</returns>
        public long GetFileSize(string file)
        {
            IntPtr hFile = new IntPtr(
                WININET.FtpOpenFile(_hConnect, file, WINAPI.GENERIC_READ, WININET.FTP_TRANSFER_TYPE_BINARY, IntPtr.Zero)
                );

            if (hFile == IntPtr.Zero)
            {
                Error();
            }
            else
            {
                try
                {
                    int sizeHigh = 0;
                    int sizeLo   = WININET.FtpGetFileSize(hFile, ref sizeHigh);

#pragma warning disable CS0675 // Bitwise-or operator used on a sign-extended operand
                    long fileSize = ((long)sizeHigh << 32) | sizeLo;
#pragma warning restore CS0675 // Bitwise-or operator used on a sign-extended operand

                    return(fileSize);
                }
                catch (Exception)
                {
                    Error();
                }
                finally
                {
                    WININET.InternetCloseHandle(hFile);
                }
            }

            return(0);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Creates a directory on the FTP server.
 /// </summary>
 /// <param name="path">A <see cref="String"/> representing the full or relative path of the directory to create.</param>
 public void CreateDirectory(string path)
 {
     if (WININET.FtpCreateDirectory(_hConnect, path) == 0)
     {
         Error();
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Checks if a file exists.
        /// </summary>
        /// <param name="path">A <see cref="String"/> representing the path to check.</param>
        /// <returns>A <see cref="Boolean"/> indicating whether the file exists.</returns>
        public bool FileExists(string path)
        {
            WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA();

            IntPtr hFindFile = WININET.FtpFindFirstFile(
                _hConnect,
                path,
                ref findData,
                WININET.INTERNET_FLAG_NO_CACHE_WRITE,
                IntPtr.Zero);

            try
            {
                if (hFindFile == IntPtr.Zero)
                {
                    return(false);
                }

                return(true);
            }
            finally
            {
                if (hFindFile != IntPtr.Zero)
                {
                    WININET.InternetCloseHandle(hFindFile);
                }
            }
        }
Exemplo n.º 12
0
        public string SendCommand(string cmd)
        {
            IntPtr ftpCommand = new IntPtr();
            int    num        = (cmd != "PASV") ? WININET.FtpCommand(_hConnect, false, 1, cmd, IntPtr.Zero, ref ftpCommand) : WININET.FtpCommand(_hConnect, false, 1, cmd, IntPtr.Zero, ref ftpCommand);
            int    capacity   = 0x2000;

            if (num == 0)
            {
                Error();
            }
            else if (ftpCommand != IntPtr.Zero)
            {
                StringBuilder buffer    = new StringBuilder(capacity);
                int           bytesRead = 0;
                while (true)
                {
                    num = WININET.InternetReadFile(ftpCommand, buffer, capacity, ref bytesRead);
                    if ((num != 1) || (bytesRead <= 1))
                    {
                        return(buffer.ToString());
                    }
                }
            }
            return("");
        }
Exemplo n.º 13
0
 public void SetCurrentDirectory(string directory)
 {
     if (WININET.FtpSetCurrentDirectory(_hConnect, directory) == 0)
     {
         Error();
     }
 }
Exemplo n.º 14
0
        public long GetFileSize(string file)
        {
            IntPtr hConnect = new IntPtr(WININET.FtpOpenFile(_hConnect, file, 0x8000_0000, 2, IntPtr.Zero));

            if (!(hConnect == IntPtr.Zero))
            {
                try
                {
                    int dwFileSizeHigh = 0;
                    int num2           = WININET.FtpGetFileSize(hConnect, ref dwFileSizeHigh);
                    return((dwFileSizeHigh << 0x20) | num2);
                }
                catch (Exception)
                {
                    Error();
                }
                finally
                {
                    WININET.InternetCloseHandle(hConnect);
                }
            }
            else
            {
                Error();
            }
            return(0L);
        }
Exemplo n.º 15
0
        private string InternetLastResponseInfo(ref int code)
        {
            int           capacity = 0x2000;
            StringBuilder buffer   = new(capacity);

            WININET.InternetGetLastResponseInfo(ref code, buffer, ref capacity);
            return(buffer.ToString());
        }
Exemplo n.º 16
0
        /// <summary>
        /// Retrieves error message text
        /// </summary>
        /// <param name="code">A <see cref="Int32"/> representing the system error code.</param>
        /// <returns>A <see cref="String"/> containing the error text.</returns>
        private string InternetLastResponseInfo(ref int code)
        {
            int BUFFER_SIZE = 8192;

            StringBuilder buff = new StringBuilder(BUFFER_SIZE);

            WININET.InternetGetLastResponseInfo(ref code, buff, ref BUFFER_SIZE);
            return(buff.ToString());
        }
Exemplo n.º 17
0
        /// <summary>
        /// Deletes a directory from the FTP server
        /// </summary>
        /// <param name="directory">A <see cref="String"/> representing the path of the directory to delete.</param>
        public void RemoveDirectory(string directory)
        {
            int ret = WININET.FtpRemoveDirectory(_hConnect, directory);

            if (ret == 0)
            {
                Error();
            }
        }
Exemplo n.º 18
0
        public string GetCurrentDirectory()
        {
            int           capacity         = 0x105;
            StringBuilder currentDirectory = new(capacity);

            if (WININET.FtpGetCurrentDirectory(_hConnect, currentDirectory, ref capacity) != 0)
            {
                return(currentDirectory.ToString());
            }
            Error();
            return(null);
        }
Exemplo n.º 19
0
 public void Open()
 {
     if (string.IsNullOrEmpty(Host))
     {
         throw new ArgumentNullException("Host");
     }
     _hInternet = WININET.InternetOpen(Environment.UserName, 0, null, null, 4);
     if (_hInternet == IntPtr.Zero)
     {
         Error();
     }
 }
Exemplo n.º 20
0
        /// <summary>
        /// Uploads a file to the FTP server
        /// </summary>
        /// <param name="fileName">A <see cref="String"/> representing the local file path to upload.</param>
        /// <param name="localFile">A <see cref="String"/> representing the file path to save the file.</param>
        public void PutFile(string localFile, string remoteFile)
        {
            int ret = WININET.FtpPutFile(_hConnect,
                                         localFile,
                                         remoteFile,
                                         WININET.FTP_TRANSFER_TYPE_BINARY,
                                         IntPtr.Zero);

            if (ret == 0)
            {
                Error();
            }
        }
Exemplo n.º 21
0
        /// <summary>
        /// Gets the current working FTP directory
        /// </summary>
        /// <returns>A <see cref="String"> representing the current working directory.</see></returns>
        public string GetCurrentDirectory()
        {
            int           buffLength = WINAPI.MAX_PATH + 1;
            StringBuilder str        = new StringBuilder(buffLength);
            int           ret        = WININET.FtpGetCurrentDirectory(_hConnect, str, ref buffLength);

            if (ret == 0)
            {
                Error();
                return(null);
            }

            return(str.ToString());
        }
Exemplo n.º 22
0
        /// <summary>
        /// Downloads a file from the FTP server to the local system
        /// </summary>
        /// <exception cref="FtpException">If the file does not exist.</exception>
        /// <param name="remoteFile">A <see cref="String"/> representing the full or relative path to the file to download.</param>
        /// <param name="localFile">A <see cref="String"/> representing the local file path to save the file.</param>
        /// <param name="failIfExists">A <see cref="Boolean"/> that determines whether an existing local file should be overwritten.</param>
        public void GetFile(string remoteFile, string localFile, bool failIfExists)
        {
            int ret = WININET.FtpGetFile(_hConnect,
                                         remoteFile,
                                         localFile,
                                         failIfExists,
                                         WINAPI.FILE_ATTRIBUTE_NORMAL,
                                         WININET.FTP_TRANSFER_TYPE_BINARY,
                                         IntPtr.Zero);

            if (ret == 0)
            {
                Error();
            }
        }
Exemplo n.º 23
0
 public void Login(string username, string password)
 {
     if (username == null)
     {
         throw new ArgumentNullException("username");
     }
     if (password == null)
     {
         throw new ArgumentNullException("password");
     }
     _hConnect = WININET.InternetConnect(_hInternet, Host, Port, username, password, 1, 0x800_0000, IntPtr.Zero);
     if (_hConnect == IntPtr.Zero)
     {
         Error();
     }
 }
Exemplo n.º 24
0
 public void Dispose()
 {
     if (!_disposed)
     {
         if (_hConnect != IntPtr.Zero)
         {
             WININET.InternetCloseHandle(_hConnect);
         }
         if (_hInternet != IntPtr.Zero)
         {
             WININET.InternetCloseHandle(_hInternet);
         }
         _hInternet = IntPtr.Zero;
         _hConnect  = IntPtr.Zero;
         _disposed  = true;
         GC.SuppressFinalize(this);
     }
 }
Exemplo n.º 25
0
        /// <summary>
        /// Establishes a connection to the host.
        /// </summary>
        /// <exception cref="ArgumentNullException">If Host is null or empty.</exception>
        public void Open()
        {
            if (String.IsNullOrEmpty(Host))
            {
                throw new ArgumentNullException("Host");
            }

            _hInternet = WININET.InternetOpen(
                System.Environment.UserName,
                WININET.INTERNET_OPEN_TYPE_PRECONFIG,
                null,
                null,
                WININET.INTERNET_FLAG_SYNC);

            if (_hInternet == IntPtr.Zero)
            {
                Error();
            }
        }
Exemplo n.º 26
0
        public bool DirectoryExists(string path)
        {
            bool flag;

            WINAPI.WIN32_FIND_DATA findFileData = new WINAPI.WIN32_FIND_DATA();
            IntPtr hInternet = WININET.FtpFindFirstFile(_hConnect, path, ref findFileData, 0x400_0000, IntPtr.Zero);

            try
            {
                flag = !(hInternet == IntPtr.Zero);
            }
            finally
            {
                if (hInternet != IntPtr.Zero)
                {
                    WININET.InternetCloseHandle(hInternet);
                }
            }
            return(flag);
        }
Exemplo n.º 27
0
 public void Dispose()
 {
     switch (_disposed)
     {
     case false:
     {
         if (_hConnect != IntPtr.Zero)
         {
             WININET.InternetCloseHandle(_hConnect);
         }
         if (_hInternet != IntPtr.Zero)
         {
             WININET.InternetCloseHandle(_hInternet);
         }
         _hInternet = IntPtr.Zero;
         _hConnect  = IntPtr.Zero;
         _disposed  = true;
         GC.SuppressFinalize(this);
         break;
     }
     }
 }
Exemplo n.º 28
0
        /// <summary>
        /// Sends a command to the FTP server
        /// </summary>
        /// <param name="cmd">A <see cref="String"/> representing the command to send.</param>
        /// <returns>A <see cref="String"/> containing the server response.</returns>
        public string SendCommand(string cmd)
        {
            int    result;
            IntPtr dataSocket = new IntPtr();

            switch (cmd)
            {
            case "PASV":
                result = WININET.FtpCommand(_hConnect, false, WININET.FTP_TRANSFER_TYPE_ASCII, cmd, IntPtr.Zero, ref dataSocket);
                break;

            default:
                result = WININET.FtpCommand(_hConnect, false, WININET.FTP_TRANSFER_TYPE_ASCII, cmd, IntPtr.Zero, ref dataSocket);
                break;
            }

            int BUFFER_SIZE = 8192;

            if (result == 0)
            {
                Error();
            }
            else if (dataSocket != IntPtr.Zero)
            {
                StringBuilder buffer    = new StringBuilder(BUFFER_SIZE);
                int           bytesRead = 0;

                do
                {
                    result = WININET.InternetReadFile(dataSocket, buffer, BUFFER_SIZE, ref bytesRead);
                } while (result == 1 && bytesRead > 1);

                return(buffer.ToString());
            }

            return("");
        }
Exemplo n.º 29
0
        public string SendCommand(string cmd)
        {
            IntPtr ftpCommand = new();
            int    num        = cmd != "PASV"
                ? WININET.FtpCommand(_hConnect, false, 1, cmd, IntPtr.Zero, ref ftpCommand)
                : WININET.FtpCommand(_hConnect, false, 1, cmd, IntPtr.Zero, ref ftpCommand);
            int capacity = 0x2000;

            switch (num)
            {
            case 0:
                Error();
                break;

            default:
            {
                if (ftpCommand != IntPtr.Zero)
                {
                    StringBuilder buffer    = new(capacity);
                    int           bytesRead = 0;
                    while (true)
                    {
                        num = WININET.InternetReadFile(ftpCommand, buffer, capacity, ref bytesRead);
                        if (num != 1 || bytesRead <= 1)
                        {
                            return(buffer.ToString());
                        }
                    }
                }

                break;
            }
            }

            return("");
        }
Exemplo n.º 30
0
        /// <summary>
        /// Gets details of all directories and their available FTP directory information from the current working FTP directory that match the directory mask.
        /// </summary>
        /// <returns>A <see cref="FtpDirectoryInfo[]"/> representing the directories in the current working directory that match the mask.</returns>
        public FtpDirectoryInfo[] GetDirectories(string path)
        {
            WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA();

            IntPtr hFindFile = WININET.FtpFindFirstFile(
                _hConnect,
                path,
                ref findData,
                WININET.INTERNET_FLAG_NO_CACHE_WRITE,
                IntPtr.Zero);

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

                if (hFindFile == IntPtr.Zero)
                {
                    if (Marshal.GetLastWin32Error() == WINAPI.ERROR_NO_MORE_FILES)
                    {
                        return(directories.ToArray());
                    }
                    else
                    {
                        Error();
                        return(directories.ToArray());
                    }
                }

                if ((findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY)
                {
                    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 WINAPI.WIN32_FIND_DATA();

                while (WININET.InternetFindNextFile(hFindFile, ref findData) != 0)
                {
                    if ((findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY)
                    {
                        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 WINAPI.WIN32_FIND_DATA();
                }

                if (Marshal.GetLastWin32Error() != WINAPI.ERROR_NO_MORE_FILES)
                {
                    Error();
                }

                return(directories.ToArray());
            }
            finally
            {
                if (hFindFile != IntPtr.Zero)
                {
                    WININET.InternetCloseHandle(hFindFile);
                }
            }
        }