2014-10-27 44 views
-1

你好我怎樣才能在我們的代碼中的驅動程序地址我們的服務器替換我們的FTP?如何從代碼連接到ftp

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp"); 

例如:

string[] filePaths = Directory.GetFiles(@"our ftp address", "*.bmp"); 

第一代碼運行良好但第二心不是工作!? TNX

+0

您正在使用'ftp://'?如果是這樣你不能使用'Directory.GetFiles()'請參閱[如何:使用FTP列出目錄內容](http://msdn.microsoft.com/en-us/library/ms229716.aspx) – 2014-10-27 13:18:48

+0

可能的重複[How在C#中使用FTP列出目錄內容?](http://stackoverflow.com/questions/3298922/how-to-list-directory-contents-with-ftp-in-c) – CodeCaster 2014-10-27 14:29:46

回答

1

來自實例http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx DisplayFileFromServer(你的bmp文件)

`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(); 
    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

這套短節目將使用由ListDirectoryDe​​tails方法的FTP服務器列表中的文件和目錄。還有一個ListDirectory方法將只列出名稱。

using System; 
using System.Net; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      WebRequest ftp = FtpWebRequest.Create("ftp://ftp.ed.ac.uk/"); 
      ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 
      using (WebResponse rsp = ftp.GetResponse()) 
      { 
       using (StreamReader reader = new StreamReader(rsp.GetResponseStream())) 
       { 
        Console.WriteLine(reader.ReadToEnd()); 
       } 
      } 
     } 
    } 
}