Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

The FtpPutFile from wininet.dll is returning false everytime i try to upload some thing to FTP.

Below is the code snippet that i am using:
C#
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FtpPutFile(IntPtr hConnect, string lpszLocalFile,
string lpszNewRemoteFile, int dwFlags, IntPtr dwContext);

//Enums for connectivity
private Int32 INTERNET_OPTION_CONNECTED_STATE  = 50;
private Int32 INTERNET_STATE_CONNECTED  = 1;
private uint INTERNET_FLAG_RELOAD = 0x80000000;
private const int InternetFlagTransferBinary = 0x00000002;


//The structure which contains the Connection States
struct INTERNET_CONNECTED_INFO
{
    public Int32 dwConnectedState;
    public Int32 dwFlags;
}

private string _ipaddress = string.Empty;
private string _username= string.Empty;
private string _password = string.Empty;


{
    try
    {
        IntPtr hSession = InternetOpen("FTP", 1, string.Empty, string.Empty, 4000000);
        IntPtr hConnect = InternetConnect(hSession, _ipaddress, 21, _username, _password, 1, 2, IntPtr.Zero);

        bool status = FtpPutFile(hConnect,localfile,remoteFile,(int)INTERNET_FLAG_RELOAD,IntPtr.Zero);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
        ErrorLog.WriteLogMsg("UploadFile(): Status: " + result);

        if (status == false)
        {
            var error = Marshal.GetLastWin32Error();

            throw new Exception(
                string.Format("UploadFile failed: {0}", error));
        }
        return status;
    }
    catch (Exception ex)
    {
        ErrorLog.WriteErrorLog(ex);
        return false;
    }
}


Please let me know why i am getting "status" as false everytime. Is there any syntax problem or something else.
Thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 21-Oct-13 3:26am    
Why doing it all when you have a code for the FTP client natively implemented in .NET?
—SA

1 solution

Please see my comment to the question. Instead, use the class System.Net.FtpWebRequest:
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900