這是我寫的ftp客戶端里用來下載服務器端目錄的函式
private void DownloadFolder(string folderPath, string downloadPath, string folderName)
{
downloadPath = downloadPath + "\\" + folderName;
setOPERATE_INFO("新建檔案夾路徑 " + downloadPath);
Directory.CreateDirectory(downloadPath);
string[] FileList = Dir(folderPath);
for (int i = 0; i < FileList.Length - 1; i++)
{
string ndownloadPath = downloadPath + "\\" + FileList[i];
setOPERATE_INFO("下載檔案路徑:" + ndownloadPath);
if (FileList[i].Contains("."))
{
setOPERATE_INFO(" 檔案 " + FileList[i]);
string afolderPath = folderPath + "\\" + FileList[i];
setOPERATE_INFO("現在的源下載位置 " + afolderPath);
string fileName = FileList[i];
openDataPort();//打開資料埠
Cmd_Line = "RETR " + afolderPath + CRLF;//復制檔案指令
BinData = System.Text.Encoding.Default.GetBytes(Cmd_Line.ToCharArray());
Cmd_Stream.Write(BinData, 0, BinData.Length);//寫入指令
getStatus();//得到資訊
//這句是我發現問題后輸出到操作資訊框除錯用的
setOPERATE_INFO("有問題的路徑 " + ndownloadPath);
//問題出在這句
FileStream fstrm = new FileStream(ndownloadPath, FileMode.OpenOrCreate);//用來將下載的檔案寫入電腦
byte[] fbytes = new byte[1030];
int cnt = 0;
while ((cnt = Data_Stream.Read(fbytes, 0, 1024)) > 0)//從網路流取得資料(二進制形式)
{
fstrm.Write(fbytes, 0, cnt);//寫入打開的檔案流
}
fstrm.Close();//關閉檔案流
closeDataPort();//關閉埠
flush_right();//重繪檔案串列
setOPERATE_INFO("成功下載檔案:" + fileName);
}
else
{
string newfolderPath = folderPath + "\\" + FileList[i];
DownloadFolder(newfolderPath, downloadPath, FileList[i]);
setOPERATE_INFO(" 檔案夾 " + FileList[i]);
}
}
return;
}

這是運行這個函式后的界面
出錯陳述句的路徑輸出在右下方的操作資訊欄里,就很奇怪,明明輸出的路徑沒有非法字符呀.....
uj5u.com熱心網友回復:
路徑前有空格
uj5u.com熱心網友回復:
請問你是說有問題的路徑哪里嗎,那個操作資訊欄里的空格是后期加上的
uj5u.com熱心網友回復:
檔案名不要自己用“+”號來拼接,應使用 System.IO.Path.Combine 方法,可以避免因環境變化、筆誤等導致的問題。uj5u.com熱心網友回復:
Dir得到的串列里還有目錄吧,用 Contains(".") 來判斷是否檔案不對,因為有的檔案可能沒有擴展名不帶".";而有的目錄可以帶".",另外還有,一個"."表示當前目錄,兩個點".."表示上級目錄,做檔案名肯定非法了uj5u.com熱心網友回復:
用@符號試一下,PAth 用+連接符號有的時候會報錯。轉載請註明出處,本文鏈接:https://www.uj5u.com/net/21163.html
標籤:C#
上一篇:游標閱讀機開發問題
下一篇:if (Component is RibbonButton button) 用法不懂 ,請求講解 ?????????
