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 服务器上创建目录?


基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 将数据集转换为列表 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01