7 Replies - 1971 Views - Last Post: 18 April 2010 - 11:49 AM Rate Topic: -----

#1 frieling  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 27-February 09

Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:06 AM

Okay,

I have tried ftpwebrequest and all of that. I cannot figure out how to connect to a ftp server without having to upload a file. I just want to see if the connection works or not.
Is This A Good Question/Topic? 0
  • +

Replies To: Connecting to FTP without uploading a file

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:26 AM

Show us what you've tried and tell us exactly what the problem is.
Was This Post Helpful? 0
  • +
  • -

#3 frieling  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 27-February 09

Re: Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:29 AM

        private void Upload(bool test)
        {
            string filename;
            if (test == true)
            {
                filename = "delete_me";
                toolStripStatusLabel1.Text = "Attempting to upload test file to server, standby.....";

                try
                {
                    File.WriteAllText(program_disk_location + "delete_me", "This is a test file by the ACI Multi Banlist Compiler program. Delete this file");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("There was an error generating test file for upload: " + ex.Message);
                    return;
                }
            }
            else
            {
                toolStripStatusLabel1.Text = "Attempting to upload banlist to server, standby.....";
                filename = textBox_banlistfn.Text;
            }
            
            string ftpServerIP = textBox_FTP_host.Text;
            string ftpUserID = textBox_FTP_username.Text;
            string ftpPassword = textBox_FTP_password.Text;
            string ftpHome = comboBox_PB_folder_paths.Text;
            string ftpPort = textBox_FTP_port.Text;
            ftpHome = ftpHome.Trim(@"/\".ToCharArray()).Replace(@"\","/");


            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + ftpServerIP + "/" + ftpHome + "/" + fileInf.Name;

            FtpWebRequest reqFTP;

            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ":" + ftpPort + "/" + ftpHome + "/" + fileInf.Name));

            // Provide the WebPermission Credintials
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

            // By default KeepAlive is true, where the control connection is not closed
            // after a command is executed.
            reqFTP.KeepAlive = false;

            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            // Specify the data transfer type.
            reqFTP.UseBinary = true;

            // Notify the server about the size of the uploaded file
            reqFTP.ContentLength = fileInf.Length;

            reqFTP.Timeout = 15000;

            // The buffer size is set to 2kb
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;

            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
            FileStream fs = fileInf.OpenRead();

            try
            {
                // Stream to which the file to be upload is written
                Stream strm = reqFTP.GetRequestStream();

                // Read from the file stream 2kb at a time
                contentLen = fs.Read(buff, 0, buffLength);

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }

                // Close the file stream and the Request Stream
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                if (test == true)
                {
                    MessageBox.Show("Error uploading test file to server: " + ex.Message);
                    toolStripStatusLabel1.Text = "Error uploading test file to server!";
                    return;
                }

                toolStripStatusLabel1.Text = "Error uploading banlist to server!";
                MessageBox.Show(ex.Message, "Upload Error");
                return;
            }

            if (test == true)
            {
                MessageBox.Show("Success!");
                toolStripStatusLabel1.Text = "Uploading testfile to server completes succesfully!";
                return;
            }

            toolStripStatusLabel1.Text = "Uploading banlist to server completed succesfully!";
        }


Thats the one i mainly tried, but it uploads a file to the server. Thats what i do not want.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:35 AM

What do you want? Use an FTP method other than Upload. Use a mode other than binary.

Here's an example of just getting the date from the server
Was This Post Helpful? 0
  • +
  • -

#5 frieling  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 27-February 09

Re: Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:38 AM

I want it were when the user enters there FTP info into the textboxes, and hits Test Connection. A message box comes up and tells the user whether the connection works, or it don't.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:40 AM

Well, look at my edited post...there's an example of getting the date from the server. That should tell you that the connection succeeded. Or you could change the method to list directory. Use your imagination!
Was This Post Helpful? 0
  • +
  • -

#7 frieling  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 27-February 09

Re: Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:41 AM

lol. Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 frieling  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 34
  • Joined: 27-February 09

Re: Connecting to FTP without uploading a file

Posted 18 April 2010 - 11:49 AM

this is what i have.


string ftpusername = txtusername.Text;
            string ftpPW = txtpw.Text;
            string ftpHost = txthost.Text;
            string ftpport = txtport.Text;
            string path = ftpusername + "/aah";

            string ftpaddress = "ftp://" + ftpHost + ":" + ftpport + "/" + path;

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpaddress);
            request.Credentials = new NetworkCredential(ftpusername, ftpPW);
            request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            MessageBox.Show("Connection works:" + response.LastModified);



and this is the error i keep getting:

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1