2011-07-27 69 views
2

我在遠程服務器的文件夾中設置了excel文件。一個WPF應用程序正在我的本地機器上運行。現在我想能夠查看遠程文件夾中的文件並下載。如何實現這一目標?我可以使用FTP訪問遠程文件夾和文件

我可以使用FTP嗎? 如果可能如何?

在此先感謝。 :)

+2

哦,來吧,你有沒有爲你自己的問題搜索? – zerkms

回答

0

這是可能的。這是一個不錯的example。和msdn documentation

FileStream outputStream = new FileStream(destinationoffile, FileMode.Create); 
FtpWebRequest request = FtpWebRequest.Create(FTPAddress + "/" + filename) as FtpWebRequest; 
request.Credentials = new NetworkCredential(username, password); 
request.UsePassive = true; 
request.Method = WebRequestMethods.Ftp.DownloadFile; 

FtpWebResponse response = request.GetResponse() as FtpWebResponse; 
Stream responseStream = response.GetResponseStream(); 
long cl = response.ContentLength; 
int bufferSize = 2048; 
int readCount; 
float perc = 0; 
float result = 0; 
float totalread = 0; 
byte[] buffer = new byte[bufferSize]; 
readCount = responseStream.Read(buffer, 0, bufferSize); 
while (readCount > 0) 
{ 
     totalread += readCount; 
     result = totalread/fileSize; 
     perc = result * 100; 
     progress.Value = perc; // this is a progressbar on my screen so may show error for you 
     outputStream.Write(buffer, 0, readCount); 
     readCount = responseStream.Read(buffer, 0, bufferSize); 
} 

responseStream.Close(); 
response.Close(); 
outputStream.Close(); // keeps file open if not closed 
+0

「簡單的FTP在WPF和.Net」---只是好奇,在這段代碼中WPF特定的是什麼?順便說一下,如果在樣本中拋出任何異常,流將不會被處置。不會推薦它作爲一個很好的例子*。 – zerkms

+0

這是可以通過簡單使用Google找到的答案類型。所以我提供了官方文檔(MSDN)和一個例子,因爲MSDN在某些時候會有些不好的例子。 FtpWebRequest在WPF中可用,所以我不明白你的抱怨是什麼。如果他問如何在WPF中添加兩個數字,你會抱怨答案「c = a + b;」不是「WPF特定的」? – colithium

+0

實例通常不提供可靠的錯誤處理是非常常見的知識。它只是掩蓋了這個問題。如果你想添加完整的和完整的錯誤處理到每個例子,然後成爲我的客人。 – colithium

相關問題