winform ftp服务器
时间: 2023-10-19 11:09:24 浏览: 273
C# winform 在线上传和浏览FTP服务器图片
您可以使用C#中的FtpWebRequest类来实现Winform FTP服务器。以下是一个简单的示例代码:
```csharp
// 设置FTP服务器地址和文件路径
string ftpServer = "ftp://ftp.example.com/";
string filePath = "example.txt";
// 设置FTP用户名和密码
string ftpUsername = "username";
string ftpPassword = "password";
// 创建FTP请求对象
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + filePath);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
// 读取本地文件并上传到FTP服务器
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader("example.txt"))
{
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
// 获取FTP服务器的响应
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine($"上传文件状态:{response.StatusDescription}");
}
```
阅读全文