- private string key1 = "File";
- private string key2 = "Folder";
- private List<string> alreadyPathed = new List<string>();
- private Dictionary<string, List<string>> getPath(string ftpURL, Dictionary<string, List<string>> retDic)
- {
- List<string> listValueFile;
- List<string> listValueFolder;
- if (retDic == null)
- {
- retDic = new Dictionary<string, List<string>>();
- listValueFile = new List<string>();
- listValueFolder = new List<string>();
- retDic.Add(key1, listValueFile);
- retDic.Add(key2, listValueFolder);
- }
- else
- {
- listValueFile = retDic[key1];
- listValueFolder = retDic[key2];
- }
- string ftpUrl = ftpURL;
- alreadyPathed.Add(ftpUrl);
- NetworkCredential nc;
- if(_config.FTPUserName == "")
- nc = new NetworkCredential();
- else
- nc = new NetworkCredential(_config.FTPUserName, _config.FTPPassword);
- WebRequest request = FtpWebRequest.Create(ftpUrl);
- request.Credentials = nc;
- request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- WebResponse response = request.GetResponse();
- StreamReader sr = new StreamReader(response.GetResponseStream());
- string strDirectory = sr.ReadToEnd();
- sr.Close();
- response.Close();
- string[] strFiles = strDirectory.Split(new string[] { "/r/n" }, StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < strFiles.Length; i++)
- {
- string tempStr = strFiles[i].Replace(" ", "");
- int tempInt = tempStr.IndexOf("<DIR>");
- string tempFolder = ftpUrl + "/";
- if (tempInt != -1)
- {
- listValueFolder.Add(tempFolder + tempStr.Substring(tempInt + 5));
- }
- else
- {
- //这里看各种文化不同,对时间的表示可能会有不同的格式,我的因为操作系统中是占去了17位,因此从17位开始取,后面
- 的就是文件名了(要取道还要再去掉空格前面的一部分无用信息)
- string tempString = strFiles[i].Substring(17).TrimStart(' ');
- int tempint = tempString.IndexOf(' ') + 1;
- listValueFile.Add(tempFolder + tempString.Substring(tempint));
- }
- }
- for (int i = 0; i < listValueFolder.Count; i++)
- {
- bool alreadyDone = alreadyPathed.Contains(listValueFolder[i]);
- if (!alreadyDone)
- getPath(listValueFolder[i], retDic);
- }
- return retDic;
- }
寻找ftp地址下所有文件的代码,包括子文件夹(c#)
最新推荐文章于 2023-12-04 15:14:53 发布