您的位置:首页 > 编程语言 > PHP开发

获取FTP服务器上以“ipva”“ap”开头,和其他文件的数目

2017-10-16 14:21 337 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Common;

namespace SendBigData_Push
{
public class FtpHelper
{
string ftpServerIP;
string ftpRemotePath;
string ftpUserID;
string ftpPassword;
string ftpURI;
string msg;
int apcount = 0;
int ipvacount = 0;
int qicount = 0;
/// <summary>
/// 连接FTP
/// </summary>
/// <param name="FtpServerIP">FTP连接地址</param>
/// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
/// <param name="FtpUserID">用户名</param>
/// <param name="FtpPassword">密码</param>
public FtpHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
ftpServerIP = FtpServerIP;
ftpRemotePath = FtpRemotePath;
ftpUserID = FtpUserID;
ftpPassword = FtpPassword;
ftpURI = "ftp://" + ftpServerIP + "/";
}
/// <summary>
/// 获取当前目录下所有的文件夹列表(仅文件夹)
/// </summary>
/// <returns></returns>
public string GetDirectoryRoot(string url)
{
apcount = 0;
ipvacount = 0;
qicount = 0;
string[] drectory = GetFilesDetailList(url);
string m = string.Empty;
foreach (string str in drectory)
{
if (str != "")
{
int dirPos = str.IndexOf("<DIR>");
if (dirPos > 0)
{
/*判断 Windows 风格*/
m += str.Substring(dirPos + 5).Trim() + "\n";
}
else if (str.Trim().Substring(0, 1).ToUpper() == "D")
{
/*判断 Unix 风格*/
string dir = str.Substring(55).Trim();
if (dir != "." && dir != "..")
{
m += dir + "\n";
}
}
else
{
if (str.Substring(55).Trim().Substring(0, 2).ToLower() == "ap")
{

4000
apcount++;
}
else if (str.Substring(55).Trim().Substring(0, 4).ToLower() == "ipva")
{
ipvacount++;
}
else
{
qicount++;
}
}
}
}
//WLog.WriteLog("SendBigData_Push", url + "下ap文件个数是:" + apcount.ToString() + ";ipva文件个数是:" + ipvacount.ToString() + "其他文件个数是:" + qicount.ToString());
char[] n = new char[] { '\n' };
string[] drectorys = m.Split(n);
foreach (string f in drectorys)
{
url = ftpURI;
if (f != "")
{
url = url + f + "/";
msg = msg + "。" + GetDirectoryList(url);
}
}
return "FTP服务器以ipva开头文件有" + ipvacount.ToString() + "个,ap开头文件有" + apcount.ToString() + "个,其他文件有" + qicount.ToString() + "个。";
}

/// <summary>
/// 获取当前目录下所有的文件夹列表(仅文件夹)
/// </summary>
/// <returns></returns>
public string GetDirectoryList(string url)
{
string[] drectory = GetFilesDetailList(url);
string m = string.Empty;
foreach (string str in drectory)
{
if (str != "")
{
int dirPos = str.IndexOf("<DIR>");
if (dirPos > 0)
{
/*判断 Windows 风格*/
m += str.Substring(dirPos + 5).Trim() + "\n";
}
else if (str.Trim().Substring(0, 1).ToUpper() == "D")
{
/*判断 Unix 风格*/
string dir = str.Substring(55).Trim();
if (dir != "." && dir != "..")
{
m += dir + "\n";
}
}
else
{
if (str.Substring(55).Trim().Substring(0, 2).ToLower() == "ap")
{
apcount++;
}
else if (str.Substring(55).Trim().Substring(0, 4).ToLower() == "ipva")
{
ipvacount++;
}
else
{
qicount++;
}
}
}
}
//WLog.WriteLog("SendBigData_Push", url + "下ap文件个数是:" + apcount.ToString() + ";ipva文件个数是:" + ipvacount.ToString() + "其他文件个数是:" + qicount.ToString());
char[] n = new char[] { '\n' };
string[] drectorys = m.Split(n);
foreach (string f in drectorys)
{
if (f != "")
{
url = url + f + "/";
msg = msg + "。" + GetDirectoryList(url);
}
}

return url + "下ap文件个数是:" + apcount.ToString() + ";ipva文件个数是:" + ipvacount.ToString() + "其他文件个数是:" + qicount.ToString();
}

/// <summary>
/// 获取当前目录下明细(包含文件和文件夹)
/// </summary>
/// <returns></returns>
public string[] GetFilesDetailList(string url)
{
string[] downloadFiles;
try
{
StringBuilder result = new StringBuilder();
FtpWebRequest ftp;
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
ftp.UsePassive = false;
WebResponse response = ftp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);

string line = reader.ReadLine();

while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
if (result.Length > 2)
result.Remove(result.ToString().LastIndexOf("\n"), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
downloadFiles = null;
throw new Exception("FtpHelper  Error --> " + ex.Message);
}
}
}
}

调用方法
/// <summary>
/// 获取Ftp信息方法
/// </summary>
public void GetFtpMessage()
{
string FtpIP="192.168.2.230:7210";
string FtpUser="test";
string FtpPassword="test";
FtpHelper ftp = new FtpHelper(cf.FtpIP, null, cf.FtpUser, cf.FtpPassword);
string describe = ftp.GetDirectoryRoot("ftp://" + cf.FtpIP + "/");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐