FTP create folder and upload files in C #

so I'm trying to automate the upload in ftp, but I can't get it to work. I have (trying to create a folder):

private void button1_Click(object sender, EventArgs e)
{
    FTPUpload(txtIP.Text, txtUName.Text, txtPWord.Text);
}

private void FTPUpload(string ftpAddress, string ftpUName, string ftpPWord)
{
    FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpAddress + "/AUTO_TEST_FOLDER"));
    ftpRequest.Credentials = new NetworkCredential(ftpUName, ftpPWord);
    ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
    WebResponse response = ftpRequest.GetResponse();
    using (var resp = (FtpWebResponse)ftpRequest.GetResponse())
    {
        MessageBox.Show(resp.StatusCode.ToString());
    }

I keep getting a WebException error. "The remote server returned an error: (550) File not available (for example, file not found, no access)." on the line WebResponse response = ftpRequest.GetResponse();.

Can anyone help me here?

I tried a couple of solutions, including the answer to How to create a directory on an ftp server using C #? but without success (without success with even copying / pasting this answer and entering my ip / uname / pword).

+3
source share
2 answers

I managed to get it to work with:

private void FtpCreateFolder(string ftpAddress, string ftpUName, string ftpPWord)
    {
            WebRequest ftpRequest = WebRequest.Create("ftp://" + ftpAddress + "/AUTO_TEST_FOLDER");
            ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
            ftpRequest.Credentials = new NetworkCredential(ftpUName, ftpPWord);
    }

, FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(...). , , , - !

+1

, , , 2 . FTP-, , , , , , . , . , . WebException, Exception , Exception. , . - (FtpWebRequest)WebRequest , .

    public static bool CreateFolder(string folder)
    {
        bool success = false;

        System.Net.FtpWebRequest ftp_web_request = null;
        System.Net.FtpWebResponse ftp_web_response = null;

        string ftp_path = @"ftp://foo.bar.com/" + folder;

        try
        {
            ftp_web_request = (FtpWebRequest)WebRequest.Create(ftp_path);
            ftp_web_request.Method = WebRequestMethods.Ftp.MakeDirectory;
            ftp_web_request.Credentials = new NetworkCredential("username", "password");

            ftp_web_response = (FtpWebResponse)ftp_web_request.GetResponse();

            string ftp_response = ftp_web_response.StatusDescription;
            string status_code = Convert.ToString(ftp_web_response.StatusCode);

            ftp_web_response.Close();

            success = true;
        }
        catch (Exception Ex)
        {
            string status = Convert.ToString(Ex);

            MessageBox.Show("Failed to create folder." + Environment.NewLine + status); //debug
        }

        return success;
    }

- , . Dictionary, - , - .

    public static bool UploadFile(string folder, Dictionary<string, string> Photo_Paths)
    {
        bool success = false;

        FtpWebRequest ftp_web_request = null;
        FtpWebResponse ftp_web_response = null;

        foreach (KeyValuePair<string, string> item in Photo_Paths)
        {
            string subdomain = ConfigurationManager.AppSettings["subdomain"];
            string ftp_path = @"ftp://foo.bar.com/" + folder + @"/" + item.Key;

            try
            {
                ftp_web_request = (FtpWebRequest)WebRequest.Create(ftp_path);
                ftp_web_request.UseBinary = true;
                ftp_web_request.UsePassive = false;
                ftp_web_request.EnableSsl = false;
                ftp_web_request.Method = WebRequestMethods.Ftp.UploadFile;
                ftp_web_request.Credentials = new NetworkCredential("username", "password");

                try
                {
                    MessageBox.Show(item.Value); //debug

                    byte[] buffer = File.ReadAllBytes(item.Value);

                    using (Stream file_stream = ftp_web_request.GetRequestStream())
                    {
                        file_stream.Write(buffer, 0, buffer.Length);
                    }

                    ftp_web_response = (FtpWebResponse)ftp_web_request.GetResponse();

                    if (ftp_web_response != null)
                    {
                        string ftp_response = ftp_web_response.StatusDescription;
                        string status_code = Convert.ToString(ftp_web_response.StatusCode);

                        MessageBox.Show(ftp_response + Environment.NewLine + status_code); //debug
                    }
                }
                catch (Exception Ex) //(WebException Ex)
                {
                    //string status = ((FtpWebResponse)Ex.Response).StatusDescription;
                    string status = Convert.ToString(Ex);

                    MessageBox.Show("Failed upload a file." + Environment.NewLine + status); //debug
                }

                ftp_web_response.Close();

                success = true;
            }
            catch (Exception Ex)
            {
                //string status = ((FtpWebResponse)Ex.Response).StatusDescription;
                string status = Convert.ToString(Ex);

                if (ftp_web_response != null)
                {
                    string ftp_response = ftp_web_response.StatusDescription;
                    string status_code = Convert.ToString(ftp_web_response.StatusCode);

                    MessageBox.Show(ftp_response + Environment.NewLine + status_code); //debug
                }

                MessageBox.Show("Failed upload a file." + Environment.NewLine + status); //debug
            }
        }

        return success;
    }
-1

All Articles