と の両方が、FTPWebClient.UploadFile
およびWebClient.DownloadFile
バイナリ ファイルに対して正しく機能します。
アップロード
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");
TLS/SSL 暗号化、ASCII/テキスト転送モード、転送再開など、WebClient
提供されていないより詳細な制御が必要な場合は、 を使用します。簡単な方法は、次を使用して FTP ストリームにコピーすることです。FtpWebRequest
FileStream
Stream.CopyTo
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}
アップロードの進行状況を監視する必要がある場合は、コンテンツを自分でチャンクごとにコピーする必要があります。
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream fileStream = File.OpenRead(@"C:\local\path\file.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("Uploaded {0} bytes", fileStream.Position);
}
}
GUI の進行状況 (WinForms ProgressBar
) については、次を参照してください:
FtpWebRequest でアップロードの進行状況バーを表示するにはどうすればよいですか?
フォルダーからすべてのファイルをアップロードする場合は
、C# での FTP サーバーへの再帰的アップロード を参照してください。
ダウンロード
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
"ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");
TLS/SSL 暗号化、ASCII/テキスト転送モード、転送の再開WebClient
など、提供されていないより詳細な制御が必要な場合は、 を使用します。簡単な方法は、FTP 応答ストリームを次のようにコピーすることです。FtpWebRequest
FileStream
Stream.CopyTo
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
ftpStream.CopyTo(fileStream);
}
ダウンロードの進行状況を監視する必要がある場合は、コンテンツを自分でチャンクごとにコピーする必要があります。
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
}
}
GUI の進行状況 (WinForms ProgressBar
) については、以下を参照してください:
FtpWebRequest ProgressBar を使用した FTP ダウンロード
リモート フォルダーからすべてのファイルをダウンロードする場合は、
C# Download all files and subdirectories through FTP を参照してください。