如何显示所有文件&使用c#从树视图中的ftp文件夹?

时间:2014-07-16 06:09:03

标签: c# ftp streamreader

我正在做Windows应用程序,在该应用程序中,我需要在树视图中显示来自FTP的所有文件和文件夹。

我做了一些示例代码,

private void button1_Click(object sender, EventArgs e)
{
    var root = "ftp://creeddemo.in";

    treeView1.Nodes.Clear();
    treeView1.Nodes.Add(CreateDirectoryNode(root, "ftp:/xxxxxxx.in/"));

}


private TreeNode CreateDirectoryNode(string root, string p)
{
    var directoryNode = new TreeNode(name);
    var directoryListing = GetDirectoryListing(path);

    var directories = directoryListing.Where(d => d.IsDirectory);
    var files = directoryListing.Where(d => !d.IsDirectory);

    foreach (var dir in directories)
    {
        directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name));
    }
    foreach (var file in files)
    {
        directoryNode.Nodes.Add(new TreeNode(file.Name));
    }
    return directoryNode;
}

public IEnumerable<FTPListDetail> GetDirectoryListing(string rootUri)
{
    var CurrentRemoteDirectory = rootUri;
    var result = new StringBuilder();

    var request = GetWebRequest(WebRequestMethods.Ftp.ListDirectoryDetails, CurrentRemoteDirectory);

    using (var response = request.GetResponse())
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            string line = reader.ReadLine();
            while (line != null)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            if (string.IsNullOrEmpty(result.ToString()))
            {
                return new List<FTPListDetail>();
            }
            result.Remove(result.ToString().LastIndexOf("\n"), 1);
            var results = result.ToString().Split('\n');
            string regex =
                @"^" +               //# Start of line
                @"(?<dir>[\-ld])" +          //# File size          
                @"(?<permission>[\-rwx]{9})" +            //# Whitespace          \n
                @"\s+" +            //# Whitespace          \n
                @"(?<filecode>\d+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<owner>\w+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<group>\w+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<size>\d+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<month>\w{3})" +          //# Month (3 letters)   \n
                @"\s+" +            //# Whitespace          \n
                @"(?<day>\d{1,2})" +        //# Day (1 or 2 digits) \n
                @"\s+" +            //# Whitespace          \n
                @"(?<timeyear>[\d:]{4,5})" +     //# Time or year        \n
                @"\s+" +            //# Whitespace          \n
                @"(?<filename>(.*))" +            //# Filename            \n
                @"$";                //# End of line

            var myresult = new List<FTPListDetail>();
            foreach (var parsed in results)
            {
                var split = new Regex(regex)
                    .Match(parsed);
                var dir = split.Groups["dir"].ToString();
                var permission = split.Groups["permission"].ToString();
                var filecode = split.Groups["filecode"].ToString();
                var owner = split.Groups["owner"].ToString();
                var group = split.Groups["group"].ToString();
                var filename = split.Groups["filename"].ToString();
                myresult.Add(new FTPListDetail()
                {
                    Dir = dir,
                    Filecode = filecode,
                    Group = group,
                    FullPath = CurrentRemoteDirectory + "/" + filename,
                    Name = filename,
                    Owner = owner,
                    Permission = permission,
                });
            };
            return myresult;
        }
    }
}

private System.Net.FtpWebRequest GetWebRequest (string method, string uri)
{

    Uri serverUri = new Uri (uri);

   //here I am getting error (An unhandled exception of type
   //--> 'System.ArgumentNullException' occurred in System.dll) <--

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return null;
    }
    var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri);
    reqFTP.Method = method;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential("aaaa", "bbbbbb@1234");
    reqFTP.Proxy = null;
    reqFTP.KeepAlive = false;
    reqFTP.UsePassive = false;
    return reqFTP;
}

帮助我离开这里

1 个答案:

答案 0 :(得分:0)

当然,您必须将“root”的值替换为FTP-Server的rootURL,并在方法GetWebRequest()中将“username”和“password”替换为您的凭证!

您需要此类来保存详细信息

public class FTPListDetail
{
        public bool IsDirectory
        {
            get
            {
                return !string.IsNullOrWhiteSpace(Dir) && Dir.ToLower().Equals("d");
            }
        }
        internal string Dir { get; set; }
        public string Permission { get; set; }
        public string Filecode { get; set; }
        public string Owner { get; set; }
        public string Group { get; set; }
        public string Name { get; set; }
        public string FullPath { get; set; }
}

以下是WinForm-App的代码隐藏

private void button1_Click(object sender, EventArgs e)
{
    var root = "ftp://ftp.yourFTPServer.at";

    treeView1.Nodes.Clear();
    treeView1.Nodes.Add(CreateDirectoryNode(root, "root"));
}

private TreeNode CreateDirectoryNode(string path, string name)
{
    var directoryNode = new TreeNode(name);
    var directoryListing = GetDirectoryListing(path);

    var directories = directoryListing.Where(d => d.IsDirectory);
    var files = directoryListing.Where(d => !d.IsDirectory);

    foreach (var dir in directories)
    {
        directoryNode.Nodes.Add(CreateDirectoryNode(dir.FullPath, dir.Name));
    }
    foreach (var file in files)
    {
            directoryNode.Nodes.Add(new TreeNode(file.Name));
    }
    return directoryNode;
}

public IEnumerable<FTPListDetail> GetDirectoryListing(string rootUri)
{
    var CurrentRemoteDirectory = rootUri;
    var result = new StringBuilder();
    var request = GetWebRequest(WebRequestMethods.Ftp.ListDirectoryDetails, CurrentRemoteDirectory);
    using (var response = request.GetResponse())
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            string line = reader.ReadLine();
            while (line != null)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            if (string.IsNullOrEmpty(result.ToString()))
            {
                return new List<FTPListDetail>();
            }
            result.Remove(result.ToString().LastIndexOf("\n"), 1);
            var results = result.ToString().Split('\n');
            string regex =
                @"^" +               //# Start of line
                @"(?<dir>[\-ld])" +          //# File size          
                @"(?<permission>[\-rwx]{9})" +            //# Whitespace          \n
                @"\s+" +            //# Whitespace          \n
                @"(?<filecode>\d+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<owner>\w+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<group>\w+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<size>\d+)" +
                @"\s+" +            //# Whitespace          \n
                @"(?<month>\w{3})" +          //# Month (3 letters)   \n
                @"\s+" +            //# Whitespace          \n
                @"(?<day>\d{1,2})" +        //# Day (1 or 2 digits) \n
                @"\s+" +            //# Whitespace          \n
                @"(?<timeyear>[\d:]{4,5})" +     //# Time or year        \n
                @"\s+" +            //# Whitespace          \n
                @"(?<filename>(.*))" +            //# Filename            \n
                @"$";                //# End of line

            var myresult = new List<FTPListDetail>();
            foreach (var parsed in results)
            {
                var split = new Regex(regex)
                    .Match(parsed);
                var dir = split.Groups["dir"].ToString();
                var permission = split.Groups["permission"].ToString();
                var filecode = split.Groups["filecode"].ToString();
                var owner = split.Groups["owner"].ToString();
                var group = split.Groups["group"].ToString();
                var filename = split.Groups["filename"].ToString();
                myresult.Add(new FTPListDetail()
                {
                    Dir = dir,
                    Filecode = filecode,
                    Group = group,
                    FullPath = CurrentRemoteDirectory + "/" + filename,
                    Name = filename,
                    Owner = owner,
                    Permission = permission,
                });
            };
            return myresult;
        }
    }
}

private FtpWebRequest GetWebRequest(string method, string uri)
{
    Uri serverUri = new Uri(uri);
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return null;
    }
    var reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverUri);
    reqFTP.Method = method;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential("yourUser", "yourPassword");
    reqFTP.Proxy = null;
    reqFTP.KeepAlive = false;
    reqFTP.UsePassive = false;
    return reqFTP;
}

enter image description here