注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

淡忘邻人

二十七,始发愤

 
 
 

日志

 
 
 
 

把FTP文件及目录添加到TreeView中 C#  

2010-05-29 21:23:59|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace EboardMedia.File.FtpUpDown
{
    public class FTPDirectFactory
    {
        private string paths = "";  //存放ftp的路径
        /// <summary>
        /// 把FTP文件及目录添加到TreeView中,
        /// 并进行单击 双击操作
        /// </summary>
        /// <returns></returns>
        public static FTPDirectFactory Create()
        {
            return new FTPDirectFactory();
        }

        public void InitTreeView(TreeView treeview)
        {
            //treeview.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeview_NodeMouseClick);
            treeview.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(treeview_NodeMouseDoubleClick);
            treeview.AfterSelect += new TreeViewEventHandler(treeview_AfterSelect);
        }

        void treeview_AfterSelect(object sender, TreeViewEventArgs e)
        {

            TreeView tvw = sender as TreeView;
            TreeNode node;
            List<string> directories;
            string str = e.Node.Text;
            FileInfo fileInfo = new FileInfo(str);
            if ((fileInfo.Extension.Length > 0) || (GetFtpSuccess.Instanse.AreFTPFileSuccess == false) || (tvw.SelectedNode.Text.Equals(tvw.Nodes[0].Text)))
            {
                return;
            }
            paths = "";
            paths = GetParent(e.Node);
            int iBegin = paths.IndexOf("/");
            int iEnd = paths.LastIndexOf("/");
            paths = paths.Substring(iBegin, iEnd - iBegin);
            node = e.Node;
            node.Nodes.Clear();
            try
            {
                directories = FTPFactory.Create().GetFileLists(paths);
                //tvw.ImageList = imgList;

                if (directories != null)
                {
                    foreach (string dir in directories)
                    {
                        fileInfo = new FileInfo(dir);
                        int j = FTPImageList.ShellImages.ImageIndex(fileInfo.Name);
                        node.Nodes.Add("100", fileInfo.Name, j, j);
                    }

                    node.ExpandAll();
                }
                if (tvw.Nodes[0].Index == 0)
                {
                    tvw.Nodes[0].ImageIndex = 19999999;
                    tvw.Nodes[0].SelectedImageIndex = 19999999;
                }
            }
            catch (Exception ex)
            {
            }
        }

        //鼠标双击打开文件
        public void treeview_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                paths = "";
                paths = GetParent(e.Node);
                int iBegin = paths.IndexOf("/");
                int iEnd = paths.LastIndexOf("/");
                paths = paths.Substring(iBegin, iEnd - iBegin);
                string NodeText = e.Node.Text;
                FTPFactory.Create().OpenFtpFile(paths, NodeText);
            }
            catch (Exception ex)
            {
            }
        }

        //返回点击节点的路径
        private string GetParent(TreeNode Node)
        {
            if (Node.Parent != null)
            {
                return GetParent(Node.Parent) + Node.Text + "/";
            }
            else
            {
                return Node.Text + "/";
            }
        }

        //获取FTP文件
        public TreeNode GetTreeviewNodes()
        {
            TreeNode Nodes = new TreeNode();
            FileInfo fileInfo = null;
            string ftppaths = String.Empty;
            List<string> directories;
            try
            {
                //初始化时 让其获取FTP最顶层文件及文件夹
                directories = FTPFactory.Create().GetFileLists(ftppaths);
                if (directories.Count == 0)
                {
                    GetFtpSuccess.Instanse.AreFTPFileSuccess = false;
                    Nodes.Nodes.Add(new TreeNode("获取文件失败", 8, 8));
                }
                else
                {
                    GetFtpSuccess.Instanse.AreFTPFileSuccess = true;
                    foreach (string dir in directories)
                    {
                        fileInfo = new FileInfo(dir);
                        int i = FTPImageList.ShellImages.ImageIndex(fileInfo.Name);
                        Nodes.Nodes.Add("10", fileInfo.Name, i, i);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return Nodes;
        }  
    }
}

  评论这张
 
阅读(919)| 评论(6)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017