Skip to content

Instantly share code, notes, and snippets.

@noqisofon
Created December 10, 2010 02:44
Show Gist options
  • Save noqisofon/735682 to your computer and use it in GitHub Desktop.
Save noqisofon/735682 to your computer and use it in GitHub Desktop.
WebClient を使って GNU の FTP サイトから coreutils を DL してくる C# のコード。
using System;
using System.IO;
using System.Net;
using System.Text;
namespace demo.ftp.downloader {
/// <summary>
///
/// </summary>
class FTPDownloadSample {
/// <summary>
///
/// </summary>
/// <param name="server_uri"></param>
/// <returns></returns>
public bool downloadFromFTPServer(Uri server_uri) {
// server_uri は ftp:// で始まる必要があります。
if ( server_uri.Scheme != Uri.UriSchemeFtp )
return false;
// サーバーとの通信に使用する WebClient オブジェクトを作成します。
WebClient request = new WebClient();
// この例では FTP サイトが匿名アカウントをサポート[要出典]していることを前提にしています。
request.Credentials = new NetworkCredential( "anonymous", "[email protected]" );
try {
byte[] new_file_data = request.DownloadData( server_uri.ToString() );
File.WriteAllBytes( Path.GetFileName( server_uri.AbsolutePath ), new_file_data );
} catch ( WebException we ) {
Console.WriteLine( we.ToString() );
}
return true;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public int run() {
Uri uri = new Uri( "ftp://ftp.gnu.org/gnu/coreutils/coreutils-8.7.tar.xz" );
downloadFromFTPServer( uri );
return 0;
}
/// <summary>
///
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args) {
FTPDownloadSample progn = new FTPDownloadSample();
progn.run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment