當前位置:編程學習大全網 - 源碼下載 - C#如何取得ftp上面的所有文件夾裏面的所有文件名,求源碼。。。。

C#如何取得ftp上面的所有文件夾裏面的所有文件名,求源碼。。。。

連接方法:

string ftpServerIP = "";

string ftpUserID = "";

string ftpPassword = "";

string TempPath = Path.GetTempPath();

FtpWebRequest reqFTP;

DataTable dt;

private void Connect(String path)//連接ftp

{

try

{

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

reqFTP.UseBinary = true;

reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

}

catch (Exception){

Response.Write("<srcipt>alert('連接失敗!')</script>");

}

獲取方法:

private string[] GetFileList(string path, string WRMethods)

{

string[] downloadFiles;

StringBuilder result = new StringBuilder();

try

{

Connect(path);

reqFTP.Method = WRMethods;

WebResponse response = reqFTP.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);

string line = reader.ReadLine();

while (line != null)

{

result.Append(line);

result.Append("\n");

line = reader.ReadLine();

}

result.Remove(result.ToString().LastIndexOf('\n'), 1);

reader.Close();

response.Close();

return result.ToString().Split('\n');

}

catch (Exception)

{

downloadFiles = null;

return downloadFiles;

}

}

調用:

string[] str = GetFileList("ftp://" + ftpServerIP + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);

數組就是ftp文件夾下的所有文件名,如果要判斷ftp根下面的是文件還是文件夾。需要解字符串才行!

  • 上一篇:速求java編程:計算器能在科學型與標準型之間切換的源代碼
  • 下一篇:12306如何搶票
  • copyright 2024編程學習大全網