我如何创建一个使用C#的FTP服务器上的目录?
问题描述:
什么是一个简单的方法来创建一个使用C#的FTP服务器上的目录?
What's an easy way to create a directory on an FTP server using C#?
我想出如何将文件上传到一个已经存在的文件夹是这样的:
I figured out how to upload a file to an already existing folder like this:
using (WebClient webClient = new WebClient())
{
string filePath = "d:/users/abrien/file.txt";
webClient.UploadFile("ftp://10.128.101.78/users/file.txt", filePath);
}
不过,如果我想上传到用户/ abrien
,我收到了 WebException
称该文件不可用。我想这是因为我需要我的上传文件之前创建新的文件夹,但 Web客户端
似乎没有任何方法来实现这一目标。
However, if I want to upload to users/abrien
, I get a WebException
saying the file is unavailable. I assume this is because I need to create the new folder before uploading my file, but WebClient
doesn't seem to have any methods to accomplish that.
答
使用的FtpWebRequest
,与WebRequestMethods.Ftp.MakeDirectory$c$c>.
例如:
using System;
using System.Net;
class Test
{
static void Main()
{
WebRequest request = WebRequest.Create("ftp://host.com/directory");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential("user", "pass");
using (var resp = (FtpWebResponse) request.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
}
}