FTP之获取路径占用大小
public void GetFtpFolerSize(ref UInt64 size, string ServerIP, string USERID, string PassWord, string path)
{
if (path == null)
path = "";
FtpWebRequest reqFtp;
try
{
reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ServerIP + "/" + path));
reqFtp.KeepAlive = true;
reqFtp.UseBinary = true;
reqFtp.UsePassive = true;
reqFtp.Credentials = new NetworkCredential(USERID, PassWord);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFtp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
List<string> listPath = new List<string>();
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line != "." && line != "..")
{
if (!line.Contains("<DIR>")) // non dir
{
int end = line.LastIndexOf(' ');
int start = line.IndexOf(" ");
string filename = line.Substring(end + 1);
if (filename.Contains("."))
{
line = line.Replace(filename, "");
size += UInt64.Parse(line.Substring(start).Trim());
}
}
else
{
string folerNm = "";
FtpClient fc = new FtpClient();
fc.ParseFolderNameFromWindowStyle(line, ref folerNm);
listPath.Add(path + "/" + folerNm);
}
}
}
reader.Close();
response.Close();
foreach (var strPath in listPath)
{
GetFtpFolerSize(ref size, ServerIP, USERID, PassWord, strPath);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}