so I'm trying to automate some uploading to an ftp, but I cannot get it to work. What I have is (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 WebException was Unhandled "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." at line WebResponse response = ftpRequest.GetResponse();.

Can someone help me out here?

I've tried a couple of solutions, including the answer at How do I create a directory on ftp server using C#?, but with no success (no success with even copy/pasting that answer and entering my ip/uname/pword).

有帮助吗?

解决方案

I managed to get it working 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);
    }

I guess the problem was using FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(...). Thanks anyway, SO, hope someone else finds this useful!

其他提示

I know this is an old thread, but I thought I would throw in my 2 cents. In the past I had the issue of getting success or fail back from an FTP server and nothing I could find really worked better than what I pulled from a test program I was using to create my actual application. It seems the big problem comes from getting a response on success or fail when trying to create a folder. The other issue comes since there are multiple Exception types. I have got both WebException and Exception at different points so I hence use a general Exception in my code. It is what works for me. The minor change from the OP is the use of (FtpWebRequest)WebRequest over what they had.

    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;
    }

The following is what I use to upload multiple files. I pass in a Dictionary where the key is the name of the file and the value is the path.

    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;
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top