Created
December 10, 2010 02:44
-
-
Save noqisofon/735682 to your computer and use it in GitHub Desktop.
WebClient を使って GNU の FTP サイトから coreutils を DL してくる C# のコード。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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