C# FTP在C中创建文件夹并上载文件#

C# FTP在C中创建文件夹并上载文件#,c#,ftp,ftpwebrequest,C#,Ftp,Ftpwebrequest,所以我试图自动上传到ftp,但我无法让它工作。我拥有的是(尝试创建文件夹): I keep get WebException was Unhandled“远程服务器返回一个错误:(550)文件不可用(例如,找不到文件,无法访问)。”在WebResponse=ftpRequest.GetResponse()行 有人能帮我吗 我尝试了几种解决方案,包括在的答案,但没有成功(甚至复制/粘贴该答案并输入我的ip/uname/pword都没有成功)。我设法让它与以下工具一起工作: private void

所以我试图自动上传到ftp,但我无法让它工作。我拥有的是(尝试创建文件夹):

I keep get WebException was Unhandled“远程服务器返回一个错误:(550)文件不可用(例如,找不到文件,无法访问)。”在
WebResponse=ftpRequest.GetResponse()行

有人能帮我吗


我尝试了几种解决方案,包括在的答案,但没有成功(甚至复制/粘贴该答案并输入我的ip/uname/pword都没有成功)。

我设法让它与以下工具一起工作:

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(…)
。无论如何,谢谢你,希望其他人觉得这有用

我知道这是一根旧线,但我想我会把我的2美分扔进去。在过去,我遇到了从FTP服务器获取成功或失败的问题,我发现没有什么比我从用于创建实际应用程序的测试程序中提取的内容更有效。似乎最大的问题来自于在尝试创建文件夹时获得成功或失败的响应。另一个问题是因为存在多种异常类型。我在不同的地方得到了
WebException
Exception
,因此我在代码中使用了一个通用的
Exception
。这对我来说很有用。OP的一个小变化是使用了
(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;
    }
下面是我用来上传多个文件的内容。我传入一个
字典
,其中键是文件名,值是路径

    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;
    }
publicstaticbool上传文件(字符串文件夹、字典照片路径)
{
布尔成功=假;
FtpWebRequest ftp_web_请求=null;
FtpWebResponse ftp_web_response=null;
foreach(照片路径中的KeyValuePair项)
{
string subdomain=ConfigurationManager.AppSettings[“subdomain”];
字符串ftp_path=@”ftp://foo.bar.com/“+文件夹+@”/“+项.键;
尝试
{
ftp_web_request=(FtpWebRequest)WebRequest.Create(ftp_路径);
ftp_web_request.UseBinary=true;
ftp\u web\u request.useAssive=false;
ftp\u web\u request.EnableSsl=false;
ftp_web_request.Method=WebRequestMethods.ftp.UploadFile;
ftp_web_request.Credentials=新的网络凭据(“用户名”、“密码”);
尝试
{
MessageBox.Show(item.Value);//调试
byte[]buffer=File.ReadAllBytes(item.Value);
使用(流文件\u Stream=ftp\u web\u request.GetRequestStream())
{
文件_stream.Write(buffer,0,buffer.Length);
}
ftp_web_response=(FtpWebResponse)ftp_web_request.GetResponse();
如果(ftp\U web\U响应!=null)
{
字符串ftp\u response=ftp\u web\u response.StatusDescription;
字符串状态\代码=Convert.ToString(ftp\ web\响应.StatusCode);
MessageBox.Show(ftp\u response+Environment.NewLine+status\u code);//调试
}
}
catch(异常Ex)/(WebException Ex)
{
//字符串状态=((FtpWebResponse)Ex.Response).StatusDescription;
字符串状态=Convert.ToString(Ex);
MessageBox.Show(“上传文件失败。”+Environment.NewLine+status);//调试
}
ftp_web_response.Close();
成功=真实;
}
捕获(例外情况除外)
{
//字符串状态=((FtpWebResponse)Ex.Response).StatusDescription;
字符串状态=Convert.ToString(Ex);
如果(ftp\U web\U响应!=null)
{
字符串ftp\u response=ftp\u web\u response.StatusDescription;
字符串状态\代码=Convert.ToString(ftp\ web\响应.StatusCode);
MessageBox.Show(ftp\u response+Environment.NewLine+status\u code);//调试
}
MessageBox.Show(“上传文件失败。”+Environment.NewLine+status);//调试
}
}
回归成功;
}

“我创建了一个完美的函数”-这正是你的观点。它并不是很完美。如果存在凭证或连接问题,此代码将接受产生的异常,显示一个消息框,说明“文件夹存在”,并返回
false
。这还远远不够完美。我从测试程序中提取了这个,所以里面的东西显然会被删除或更改。我还修复了旧版本代码中“文件夹存在”的消息。理想情况下,我会使用WebException,然后看看确切的问题是什么。是的,那么这个答案的意义是什么?您已经将现有答案包装在一个try-catch块中,并发布了从一些特定于应用程序的变量上传照片的方法?如果第一个文件成功,但第二个文件没有成功,则第二个代码返回false。它会将整个文件加载到内存中(即使是大文件),但不会进行缩放。库方法不应该捕获异常,也绝对不应该显示消息框。同样,只需要从我的测试程序中提取并修改一些特定用途的东西。有一件事对我个人有帮助,那就是看到人们做事的各种不同方式。即使只有很小的差别。没有帮助的是你表现出的消极情绪。你可以不粗鲁地评论。被建设性的批评冒犯是一种选择(换句话说,我看不出我在哪里粗鲁)。我只是想提醒未来的读者,这本书所涉及的问题
    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;
    }