使用C# 在ftp服务器创建多级目录
时间: 2025-06-14 20:18:01 浏览: 11
在C#中,通过System.IO.FtpClient类可以实现FTP上传文件并创建多级目录。以下是一个基本示例,展示如何使用FtpWebRequest对象来创建多级目录:
```csharp
using System;
using System.IO;
using System.Net;
public void CreateDirectoryFtp(string ftpServer, string username, string password, string directoryPath)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"ftp://{ftpServer}/{directoryPath}");
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(username, password);
request.UseBinary = true; // 设置为二进制模式,适用于处理文件
try
{
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine("Directory created successfully: {0}", directoryPath);
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to create directory: {ex.Message}");
}
}
// 调用函数并指定服务器信息和路径
string server = "your_ftp_server";
string user = "your_username";
string pass = "your_password";
string dirToCreate = "dir1/subdir1"; // 指定要创建的多级目录结构
.CreateDirectoryFtp(server, user, pass, dirToCreate);
阅读全文
相关推荐
















