Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

ftpgetfile (wininet)
 
.
Summary
Retrieves a file from the FTP server and stores it under the specified file name, creating a new local file in the process.

C# Signature:

[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FtpGetFile(IntPtr hConnect, string remoteFile,
   string newFile, [MarshalAs(UnmanagedType.Bool)] bool failIfExists,
   int flagsAndAttributes, int flags, IntPtr context);

VB Signature:

Declare Function FtpGetFile Lib "wininet.dll" (ByVal hConnect As IntPtr, _
   ByVal remoteFile As String, ByVal newFile As String, _
   <MarshalAs(UnmanagedType.Bool)> ByVal failIfExists As Boolean, _
   ByVal flagsAndAttributes As Integer, ByVal flags As Integer, _
   ByVal context As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean

User-Defined Types:

None.

Notes:

Useful flags setting for binary download: INTERNET_FLAG_TRANSFER_BINARY + INTERNET_FLAG_RELOAD

Tips & Tricks:

Please add some!

Sample Code:

    public class FTPClient
    : IDisposable
    {
    #region Ctors
    public FTPClient()
    {
        FTPPort = INTERNET_DEFAULT_FTP_PORT;
        internetHandle = IntPtr.Zero;
        ftpHandle = IntPtr.Zero;
    }

    ~FTPClient()
    {
        Dispose();
    }
    #endregion

    #region P/Invoke
    [DllImport("wininet.dll", EntryPoint = "InternetConnectW", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr InternetConnectW(
        IntPtr hInternet,
        string ServerName,
        ushort ServerPort,
        string sUserName,
        string sPassword,
        uint nService,
        uint nFlags,
        uint nContext);

    [DllImport("wininet.dll", EntryPoint = "InternetOpenW", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr InternetOpenW(
        string sAgent,
        uint nAccessType,
        string sProxy,
        string sProxyBypass,
        uint nFlags);

    [DllImport("wininet.dll", EntryPoint = "FtpPutFile")]
    public static extern bool FtpPutFile(
        IntPtr hFtpConn,
        string lpszLocalFile,
        string lpszNewRemoteFile,
        uint dwFlags,
        uint dwContext);

    [DllImport("wininet.dll", EntryPoint = "FtpGetFile", SetLastError = true, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool FtpGetFile(
        IntPtr hFtpConn,
        string strSource,
        string strTarget,
        [MarshalAs(UnmanagedType.Bool)] bool bFailIfExists,
        uint nFlagsAndAtrribute,
        uint nFlags,
        uint nContext);

    [DllImport("wininet.dll", EntryPoint = "FtpOpenFile", SetLastError = true, CharSet = CharSet.Auto)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern uint FtpOpenFile(
        IntPtr hFtpConn,
        string strFileName,
        uint nAccess,
        uint nFlags,
        uint nContext);

    [DllImport("wininet.dll", EntryPoint = "InternetCloseHandle")]
    public static extern long InternetCloseHandle(IntPtr hInet);

    [DllImport("wininet.dll", EntryPoint = "GetLastError")]
    public static extern long GetLastError();
    #endregion

    #region Members
    private const uint INTERNET_OPEN_TYPE_PRECONFIG = 0;
    private const long INTERNET_OPEN_TYPE_DIRECT = 1;
    private const long INTERNET_OPEN_TYPE_PROXY = 3;
    private const long INTERNET_FLAG_RELOAD = 0x80000000;
    private const ushort INTERNET_DEFAULT_FTP_PORT = 21;
    private const uint INTERNET_FLAG_PASSIVE = 0x08000000;
    private const uint INTERNET_SERVICE_FTP = 1;
    private const uint FILE_ATTRIBUTE_NORMAL = 128;

    private const string IPADDRESS_PATTERN = @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";

    private IntPtr internetHandle;

    private IntPtr ftpHandle;
    #endregion

    #region Properties
    public ushort FTPPort { get; set; }

    public string FTPSite { get; set; }

    public string FTPUsername { get; set; }

    public string FTPPassword { get; set; }
    #endregion

    #region Methods
    private void validateFTP()
    {
        if (FTPSite == string.Empty)
        throw new InvalidOperationException(Resources.FTPClient_NoFTPSiteSpecified);
    }

    public void Connect()
    {
        validateFTP();
        if (internetHandle == IntPtr.Zero)
        {
        internetHandle = InternetOpenW("FTPOpen", INTERNET_OPEN_TYPE_PRECONFIG, string.Empty, string.Empty, 0);

        if (internetHandle == IntPtr.Zero)
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }

        if (ftpHandle == IntPtr.Zero)
        {
        ftpHandle = InternetConnectW(internetHandle, FTPSite,
                    FTPPort, FTPUsername, FTPPassword,
                    INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);

        if (ftpHandle == IntPtr.Zero)
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
    }

    public void Disconnect()
    {
        try
        {
        if (ftpHandle != IntPtr.Zero)
            InternetCloseHandle(ftpHandle);
        }
        catch
        {
        }
        finally
        {
        ftpHandle = IntPtr.Zero;
        }
        try
        {
        if (internetHandle != IntPtr.Zero)
            InternetCloseHandle(internetHandle);
        }
        catch
        {
        }
        finally
        {
        internetHandle = IntPtr.Zero;
        }
    }

    public void PutFileOnFTPServer(FTPFile file)
    {
        Connect();
        if (!File.Exists(file.SourceFileName))
        throw new InvalidOperationException(Resources.FTPClient_HDSourceFileDoesNotExist);
        if (!FtpPutFile(ftpHandle, file.SourceFileName, file.DesctinationFileName, (uint)file.TransferType, 0))
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void GetFileFromFTPServer(FTPFile file)
    {
        Connect();
        if (!FtpGetFile(ftpHandle, file.SourceFileName, file.DesctinationFileName, false,
                FILE_ATTRIBUTE_NORMAL, (uint)file.TransferType, 0))
        throw new Win32Exception(Marshal.GetLastWin32Error());
    }

    public void CloseFTP()
    {
        Disconnect();
    }
    #endregion

    #region Inner classes
    public class FTPFile
    {
        #region Ctors
        public FTPFile()
        {
        }

        public FTPFile(string sourceFileName, string destinationFileName, FTPTransferType transferType)
        : this()
        {
        SourceFileName = sourceFileName;
        DesctinationFileName = destinationFileName;
        TransferType = transferType;
        }
        #endregion

        #region Members
        public enum FTPTransferType
        : uint
        {
        Auto = 0,
        ASCII = 1,
        Binary = 2
        }
        #endregion

        #region Properties
        public string SourceFileName { get; set; }

        public string DesctinationFileName { get; set; }

        public FTPTransferType TransferType { get; set; }
        #endregion

        #region Overrides
        #endregion

        #region Methods
        #endregion
    }
    #endregion

    #region IDisposable Members
    public void Dispose()
    {
        CloseFTP();
        GC.SuppressFinalize(this);
    }

    #endregion
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
FtpGetFile on MSDN

Direct Link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/FtpGetFile.asp

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions