ftpclient c#代碼咋寫

c#
小樊
81
2024-10-18 20:04:40

在C#中,你可以使用FtpClient類來(lái)連接到FTP服務(wù)器并執(zhí)行文件傳輸操作。以下是一個(gè)簡(jiǎn)單的示例代碼,展示了如何使用C#的FtpClient類連接到FTP服務(wù)器并上傳文件:

using System;
using System.IO;
using System.Net;

class FtpClientExample
{
    static void Main()
    {
        // FTP服務(wù)器的地址和端口
        string server = "ftp.example.com";
        int port = 21;

        // FTP用戶名和密碼
        string username = "your_username";
        string password = "your_password";

        // 要上傳的文件路徑
        string localFilePath = @"C:\path\to\your\file.txt";

        // 創(chuàng)建FtpClient對(duì)象
        FtpClient ftpClient = new FtpClient(server, port, username, password);

        try
        {
            // 連接到FTP服務(wù)器
            ftpClient.Connect();

            // 檢查連接是否成功
            if (ftpClient.IsConnected)
            {
                Console.WriteLine("Connected to FTP server.");

                // 設(shè)置文件傳輸模式為二進(jìn)制,以防止文件損壞
                ftpClient.BinaryMode = true;

                // 上傳文件
                using (FileStream fileStream = new FileStream(localFilePath, FileMode.Open))
                {
                    ftpClient.UploadFile(fileStream, Path.GetFileName(localFilePath));
                    Console.WriteLine("File uploaded successfully.");
                }
            }
            else
            {
                Console.WriteLine("Failed to connect to FTP server.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            // 斷開與FTP服務(wù)器的連接
            ftpClient.Disconnect();
        }
    }
}

請(qǐng)注意,上述代碼示例中的FtpClient類并不是.NET框架自帶的。你需要使用第三方庫(kù),如FluentFTP,來(lái)實(shí)現(xiàn)FTP客戶端功能。以下是使用FluentFTP庫(kù)的示例代碼:

首先,通過NuGet包管理器安裝FluentFTP庫(kù):

Install-Package FluentFTP

然后,使用以下代碼連接到FTP服務(wù)器并上傳文件:

using System;
using System.IO;
using FluentFTP;

class FtpClientExample
{
    static void Main()
    {
        // FTP服務(wù)器的地址和端口
        string server = "ftp.example.com";
        int port = 21;

        // FTP用戶名和密碼
        string username = "your_username";
        string password = "your_password";

        // 要上傳的文件路徑
        string localFilePath = @"C:\path\to\your\file.txt";

        // 創(chuàng)建FtpClient對(duì)象
        FtpClient ftpClient = new FtpClient(server, port, username, password);

        try
        {
            // 連接到FTP服務(wù)器
            ftpClient.Connect();

            // 檢查連接是否成功
            if (ftpClient.IsConnected)
            {
                Console.WriteLine("Connected to FTP server.");

                // 設(shè)置文件傳輸模式為二進(jìn)制,以防止文件損壞
                ftpClient.BinaryMode = true;

                // 上傳文件
                using (FileStream fileStream = new FileStream(localFilePath, FileMode.Open))
                {
                    ftpClient.UploadFile(fileStream, Path.GetFileName(localFilePath));
                    Console.WriteLine("File uploaded successfully.");
                }
            }
            else
            {
                Console.WriteLine("Failed to connect to FTP server.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            // 斷開與FTP服務(wù)器的連接
            ftpClient.Disconnect();
        }
    }
}

請(qǐng)注意,上述代碼示例中的FtpClient類是FluentFTP庫(kù)中的類。你需要使用該庫(kù)來(lái)實(shí)現(xiàn)FTP客戶端功能。

0