c# ftp上传文件夹里面符合bmp的文件
以下是一个使用C#上传文件夹中符合*.bmp文件的示例代码:
using System;
using System.IO;
using System.Net;
namespace FTPUploadFolder
{
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\path\to\folder"; // 文件夹路径
string ftpServer = "ftp://example.com"; // FTP服务器地址
string username = "username"; // FTP用户名
string password = "password"; // FTP密码
// 获取文件夹中所有符合*.bmp的文件
string[] files = Directory.GetFiles(folderPath, "*.bmp");
foreach (string file in files)
{
// 创建FTP请求
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + "/" + Path.GetFileName(file));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
// 读取本地文件内容
byte[] fileContents = File.ReadAllBytes(file);
// 上传文件内容
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
// 获取FTP服务器的响应
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine($"File {file} uploaded successfully. Response: {response.StatusDescription}");
response.Close();
}
Console.ReadLine();
}
}
}
在上述代码中,你需要将folderPath替换为你要上传的文件夹的路径,将ftpServer替换为你的FTP服务器地址,以及提供有效的FTP用户名和密码。该代码将遍历文件夹中的所有*.bmp文件,并使用FTP协议将每个文件上传到指定的FTP服务器
原文地址: https://www.cveoy.top/t/topic/hPX7 著作权归作者所有。请勿转载和采集!