Home >Backend Development >C++ >How can I upload and download files to/from an FTP server using C#/.NET?

How can I upload and download files to/from an FTP server using C#/.NET?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-11 07:31:45562browse

How can I upload and download files to/from an FTP server using C#/.NET?

Upload and download FTP server files using C#/.NET

This article describes how to use C#/.NET to efficiently upload and download FTP server files. We'll provide several methods, including using WebClient and FtpWebRequest, and show how to track upload and download progress.

File upload

Method 1: Use WebClient (simple method)

Here’s the easy way to upload your files:

<code class="language-csharp">WebClient client = new WebClient();
client.Credentials = new NetworkCredential("用户名", "密码");
client.UploadFile("ftp://ftp.example.com/远程路径/文件.zip", @"C:\本地路径\文件.zip");</code>

Method 2: Use FtpWebRequest (stream copy, more fine control)

This method allows finer control over the upload process, achieved via stream replication:

<code class="language-csharp">FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/远程路径/文件.zip");
request.Credentials = new NetworkCredential("用户名", "密码");
request.Method = WebRequestMethods.Ftp.UploadFile;

using (Stream fileStream = File.OpenRead(@"C:\本地路径\文件.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    fileStream.CopyTo(ftpStream);
}</code>

Method 3: Use FtpWebRequest (blocked copy, progress tracking)

This method copies the file content in chunks to track the upload progress:

<code class="language-csharp">FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/远程路径/文件.zip");
request.Credentials = new NetworkCredential("用户名", "密码");
request.Method = WebRequestMethods.Ftp.UploadFile;

using (Stream fileStream = File.OpenRead(@"C:\本地路径\文件.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        ftpStream.Write(buffer, 0, read);
        Console.WriteLine("已上传 {0} 字节", fileStream.Position);
    }
}</code>

File download

Method 1: Use WebClient (simple method)

Here’s an easy way to download your file:

<code class="language-csharp">WebClient client = new WebClient();
client.Credentials = new NetworkCredential("用户名", "密码");
client.DownloadFile("ftp://ftp.example.com/远程路径/文件.zip", @"C:\本地路径\文件.zip");</code>

Method 2: Use FtpWebRequest (stream copy, more fine control)

This method allows finer control over the download process, achieved via stream replication:

<code class="language-csharp">FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/远程路径/文件.zip");
request.Credentials = new NetworkCredential("用户名", "密码");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\本地路径\文件.zip"))
{
    ftpStream.CopyTo(fileStream);
}</code>

Method 3: Use FtpWebRequest (blocked copy, progress tracking)

This method copies the file content in chunks to track the download progress:

<code class="language-csharp">FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/远程路径/文件.zip");
request.Credentials = new NetworkCredential("用户名", "密码");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\本地路径\文件.zip"))
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fileStream.Write(buffer, 0, read);
        Console.WriteLine("已下载 {0} 字节", fileStream.Position);
    }
}</code>

Remember to replace "用户名", "密码", "ftp://ftp.example.com/远程路径/文件.zip", and @"C:本地路径文件.zip" with your actual FTP server information and file path.

The above is the detailed content of How can I upload and download files to/from an FTP server using C#/.NET?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn