使用 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);
}
但是,如果我想上传到 users/abrien
,我会收到一个 WebException
说文件不可用.我认为这是因为我需要在上传文件之前创建新文件夹,但 WebClient
似乎没有任何方法可以实现.
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
.
Use FtpWebRequest
, with a method of WebRequestMethods.Ftp.MakeDirectory
.
例如:
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);
}
}
}
这篇关于如何使用 C# 在 FTP 服务器上创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!