c# - FTP创建文件夹并在C#中上传文件

标签 c# ftp ftpwebrequest

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

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

我不断收到 WebException 未处理“远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问)。”在行 WebResponse response = ftpRequest.GetResponse();

有人可以帮我吗?

我尝试了几种解决方案,包括 How do I create a directory on ftp server using C#? 中的答案,但没有成功(即使复制/粘贴该答案并输入我的 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(...)。无论如何,谢谢,希望其他人觉得这很有用!

关于c# - FTP创建文件夹并在C#中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666412/

相关文章:

c# - 具有多个创建 FTP 请求的线程的 ThreadPool 超时

c# - 从事件处理程序确定发件人对象

git - 如何将代码从 Cloudways 推送到 Github

c# - 使用 csvhelper 和 memorystream 时字节长度错误

c# - 从 FTP 目录读取文件夹路径到 IEnumerable

Java 和 FTP 编辑在线文本文件

c# - System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath 对于单元测试为 null

c# - 当只需要几个方法时,有什么方法可以避免创建巨大的 C# COM 接口(interface)包装器?

c# - ForEach TreeNode.Nodes 取出空节点

php ftp_get 在失败时删除本地文件