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

43 篇文章 0 订阅
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")
                        {
                            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 + "/");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值