2012-06-20 211 views
1

我需要知道一种连接到FTP站点的方式,我无法找到一个使用C#执行程序的示例。 我需要编写我可以连接的代码,并从FTP服务器下载文件而不使用第三方组件。连接到FTP服务器并从FTP下载文件到本地机器

我该怎么做?帮帮我。

+0

第三方组件将被关闭代码和开源? –

回答

4

有一流的FtpWebRequest .NET 4的

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

最后有一些例子。这里是一个取自msdn的样本:

public static bool DisplayFileFromServer(Uri serverUri) 
{ 
    // The serverUri parameter should start with the ftp:// scheme. 
    if (serverUri.Scheme != Uri.UriSchemeFtp) 
    { 
     return false; 
    } 
    // Get the object used to communicate with the server. 
    WebClient request = new WebClient(); 

    // This example assumes the FTP site uses anonymous logon. 
    request.Credentials = new NetworkCredential ("anonymous","[email protected]"); 
    try 
    { 
     byte [] newFileData = request.DownloadData (serverUri.ToString()); 
     string fileString = System.Text.Encoding.UTF8.GetString(newFileData); 
     Console.WriteLine(fileString); 
    } 
    catch (WebException e) 
    { 
     Console.WriteLine(e.ToString()); 
    } 
    return true; 
} 
+0

感谢您的关注。 – Urvashi

相关问题