locked
Trying to create a program for logon and get files from FTP RRS feed

  • Question

  • I'm hoping this is the correct forum.

    I am trying to create a program in Visual Studio that will run as an executable file.  Basically the scenario is, the program would open to a login screen.  The individual user would input there username and password and click on "connect" or "login" and it would connect to the FTP server with that users credentials.  The next process would be to download all files or any new files.

    What would the proper coding be for this?

     

    Thursday, July 25, 2019 2:08 PM

All replies


  • What would the proper coding be for this?

     

    Hi

    The proper coding would start with whatever code you have developed so far - please show it. (in a Code Block)


    Regards Les, Livingston, Scotland

    Thursday, July 25, 2019 2:16 PM
  • The basics is to have a form with user name and password TextBox controls which as passed to code similar this.

    My.Computer.Network.DownloadFile("ftp://someserver/somefile", "replace with your file name", UserNameTextBox.Text, UserPasswordTextBox.Text)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, July 25, 2019 3:54 PM
  • Hi,

    try the code:

    Sub getFileFromFTP(ByVal localFile As String, ByVal remoteFile As String, ByVal host As String, ByVal username As String, ByVal password As String)
            Dim URI As String = host & remoteFile
            Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
            ftp.Credentials = New System.Net.NetworkCredential(username, password)
            ftp.KeepAlive = False
            ftp.UseBinary = False
            ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
            Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
                Using responseStream As IO.Stream = response.GetResponseStream
                    Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
                        Dim buffer(2047) As Byte
                        Dim read As Integer = 0
                        Do
                            read = responseStream.Read(buffer, 0, buffer.Length)
                            fs.Write(buffer, 0, read)
                        Loop Until read = 0 'see Note(1)
                        responseStream.Close()
                        fs.Flush()
                        fs.Close()
                    End Using
                    responseStream.Close()
                End Using
                response.Close()
            End Using
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 26, 2019 1:26 AM
  • public void getFileFromFTP(string local, string remote, string host, string username, string password)
    {
        string URL = host + remote;
        System.Net.FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(URL);
        ftp.Credentials = new System.Net.NetworkCredential(username, password);
        ftp.KeepAlive = false;
        ftp.UseBinary = false;
        ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
        using (System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)ftp.GetResponse())
        {
            using (System.IO.Stream responseStream = response.GetResponseStream())
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(local, System.IO.FileMode.Create))
                {
                    byte[] buffer = new byte[4096];
                    int read = 0;
                    do
                    {
                        read = responseStream.Read(buffer, 0, buffer.Length);
                        fs.Write(buffer, 0, read);
                    }
                    while (!read == 0); // see Note(1)
                    responseStream.Close();
                    fs.Flush();
                    fs.Close();
                }
                responseStream.Close();
            }
            response.Close();
        }
    }
    Hi,
    Try mine if you are using C#. 
    In addition, if your needs are not high, you can use the commercial programs available. ATP is an example, its trial version has no time limit and full functionality for you to use. If you have a higher budget, try Chilkat
    Friday, July 26, 2019 7:29 AM