2011-06-14 91 views
-2

我是新的c#。我想知道在這段代碼中我應該改變什麼來連接到我的ftp服務器。任何人都可以幫助我嗎?我無法理解string fileName,Uri serverUri,long offset,所以請幫助我。如何使用此Ftp代碼

ftphost address= localhost 
username = test 
password = test 
filename = test.zip 

    public static bool RestartDownloadFromServer(string fileName, Uri serverUri, long offset) 
    { 
     // The serverUri parameter should use the ftp:// scheme. 
     // It identifies the server file that is to be downloaded 
     // Example: ftp://contoso.com/someFile.txt. 

     // The fileName parameter identifies the local file. 
     //The serverUri parameter identifies the remote file. 
     // The offset parameter specifies where in the server file to start reading data. 

     if (serverUri.Scheme != Uri.UriSchemeFtp) 
     { 
      return false; 
     } 
     // Get the object used to communicate with the server. 
     FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); 
     request.Method = WebRequestMethods.Ftp.DownloadFile; 
     request.ContentOffset = offset; 
     FtpWebResponse response = null; 
     try 
     { 
      response = (FtpWebResponse)request.GetResponse(); 
     } 
     catch (WebException e) 
     { 
      Console.WriteLine(e.Status); 
      Console.WriteLine(e.Message); 
      return false; 
     } 
     // Get the data stream from the response. 
     Stream newFile = response.GetResponseStream(); 
     // Use a StreamReader to simplify reading the response data. 
     StreamReader reader = new StreamReader(newFile); 
     string newFileData = reader.ReadToEnd(); 
     // Append the response data to the local file 
     // using a StreamWriter. 
     StreamWriter writer = File.AppendText(fileName); 
     writer.Write(newFileData); 
     // Display the status description. 

     // Cleanup. 
     writer.Close(); 
     reader.Close(); 
     response.Close(); 
     Console.WriteLine("Download restart - status: {0}", response.StatusDescription); 
     return true; 
    } 

Thanks in Addvance。

+0

你需要去改變它呢?如果是,爲什麼? – 2011-06-14 03:21:48

+0

如果你想學習c# - 從閱讀書籍和回顧**簡單**例子開始。如果你需要完成你的工作 - 僱傭一些工作。 – zerkms 2011-06-14 03:22:59

+0

如果你沒有很快闡述,你的問題將被視爲「不是真正的問題」。請快速闡述你的意思:你不懂'string fileName'和其他參數? – 2011-06-14 03:23:50

回答

2

在你的代碼示例中是這些詞你還需要什麼。

// serverUri參數應使用ftp://方案。 //標識要下載的服務器文件 //示例:ftp://contoso.com/someFile.txt。 // fileName參數標識本地文件。 // serverUri參數標識遠程文件。 //偏移參數指定服務器文件開始讀取數據的位置。

+1

'6,666'用upvote改變這樣偉大的聲望值是如此傷心;-) – zerkms 2011-06-14 03:27:14

+0

有一個downvote。 – phooji 2011-06-14 03:27:51

+0

是啊:) Awww什麼過山車 – rerun 2011-06-14 03:29:06

3

該方法開始的評論是綽綽有餘的。

呼叫踢東西了方法:

RestartDownloadFromServer("ftp://localhost/test.zip", "c:\test.zip", 0); 

你不使用用戶名/密碼應付的例子。爲此,您需要創建一個NetworkCredential並將其添加到WebRequest。

+0

謝謝先生,你可以告訴我如何添加用戶名和密碼,因爲我需要Secuity下載文件。 – 2011-06-14 03:32:03