首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用.NET ,如何FTP到服务器,获取文件和删除文件?

在使用.NET框架进行FTP操作时,可以使用System.Net.FtpWebRequest类来实现文件的上传、下载和删除等操作。下面是一个简单的示例,展示了如何使用.NET框架进行FTP操作:

代码语言:csharp
复制
using System;
using System.IO;
using System.Net;
using System.Text;

public class FtpClient
{
    private string _ftpServerIP;
    private string _ftpUserID;
    private string _ftpPassword;

    public FtpClient(string ftpServerIP, string ftpUserID, string ftpPassword)
    {
        _ftpServerIP = ftpServerIP;
        _ftpUserID = ftpUserID;
        _ftpPassword = ftpPassword;
    }

    public void UploadFile(string localFilePath, string remoteFilePath)
    {
        try
        {
            FileInfo fileInfo = new FileInfo(localFilePath);
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"ftp://{_ftpServerIP}/{remoteFilePath}");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(_ftpUserID, _ftpPassword);
            request.ContentLength = fileInfo.Length;
            request.UseBinary = true;
            request.KeepAlive = false;

            using (FileStream fs = fileInfo.OpenRead())
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    byte[] buffer = new byte[4096];
                    int bytesRead = 0;

                    while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        requestStream.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error uploading file: {ex.Message}");
        }
    }

    public void DownloadFile(string remoteFilePath, string localFilePath)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"ftp://{_ftpServerIP}/{remoteFilePath}");
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(_ftpUserID, _ftpPassword);
            request.UseBinary = true;
            request.KeepAlive = false;

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (FileStream fs = new FileInfo(localFilePath).Create())
                    {
                        byte[] buffer = new byte[4096];
                        int bytesRead = 0;

                        while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            fs.Write(buffer, 0, bytesRead);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error downloading file: {ex.Message}");
        }
    }

    public void DeleteFile(string remoteFilePath)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"ftp://{_ftpServerIP}/{remoteFilePath}");
            request.Method = WebRequestMethods.Ftp.DeleteFile;
            request.Credentials = new NetworkCredential(_ftpUserID, _ftpPassword);
            request.UseBinary = true;
            request.KeepAlive = false;

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                Console.WriteLine($"File deleted successfully.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error deleting file: {ex.Message}");
        }
    }
}

在这个示例中,我们创建了一个名为FtpClient的类,该类包含了三个方法:UploadFileDownloadFileDeleteFile,分别用于上传文件、下载文件和删除文件。这些方法使用了System.Net.FtpWebRequest类来实现FTP操作。

要使用这个类,你需要提供FTP服务器的IP地址、用户名和密码,然后创建一个FtpClient实例,并调用相应的方法来执行FTP操作。例如:

代码语言:csharp
复制
FtpClient ftpClient = new FtpClient("192.168.1.1", "username", "password");
ftpClient.UploadFile("C:\\localfile.txt", "remotefile.txt");
ftpClient.DownloadFile("remotefile.txt", "C:\\localfile.txt");
ftpClient.DeleteFile("remotefile.txt");

请注意,这个示例仅用于演示目的,实际应用中可能需要进行更多的错误处理和异常处理。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券