How do I create a directory on FTP server using C#?(如何使用 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);
}
但是,如果我想上传到 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 服务器上创建目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 C# 在 FTP 服务器上创建目录?


- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01