Winform FTP 文档管理上传下载-初始化TreeView,增加、重命名、删除文件夹(二)

来源:互联网 发布:ipad pro 知乎 编辑:程序博客网 时间:2024/06/05 16:46

1.承接上文,新建WInform 窗体,在连接成功以后,隐藏登录界面,显示窗体2

  private void btbLogin_Click(object sender, EventArgs e) {            bool result = CheckConnect(tbFTP.Text, tbuser.Text, tbpwd.Text);            if (result) {                Properties.Settings.Default.FTP = tbFTP.Text;                Properties.Settings.Default.Root = tbrootpath.Text;                Properties.Settings.Default.User = tbuser.Text;                Properties.Settings.Default.Password = tbpwd.Text;                if (checkBox1.Checked == true) {                    Properties.Settings.Default.Isremember = true;                } else {                    Properties.Settings.Default.Isremember = false;                }                           Properties.Settings.Default.Save();                Properties.Settings.Default.Reload();                this.Hide();                //    MessageBox.Show("连接成功");                Form2 f = new Form2();                f.Show();            } else {                tbuser.Text = "";                tbpwd.Text = "";                checkBox1.Checked = false;                MessageBox.Show("连接失败");            }                    }
2. 设计窗体2 ,TreeView 控件、ImageList控件, treeView  用于展示FTP服务器上文件夹,Imagelist 用于节点的样式。在TreeView 属性中将imaglist和treeView 绑定


3.声明几个全局变量

  static string ftpIP = Properties.Settings.Default.FTP;        static string ftpUser = Properties.Settings.Default.User;        static string ftpPsw = Properties.Settings.Default.Password;        FTPHelper ftpHelper = new FTPHelper(ftpIP, ftpUser, ftpPsw);
4.定义初始化TreeView 方法

  //初始化Tree        private void InitTree(string path) {            treeView1.Nodes.Clear();            TreeNode root = new TreeNode();            root.Text = "主目录";            treeView1.Nodes.Add(root);            List<string> sr = ftpHelper.GetDirctory(path);            foreach (var item in sr) {                TreeNode tchild = new TreeNode();                tchild.Text = item;                tchild.ImageIndex = 0;                LoadAll(tchild, path + item + "/");                root.Nodes.Add(tchild);//把根节点加入到treeview的根节点            }        }        #region                //递归查询根目录下的所有文件夹        public void LoadAll(TreeNode tn, string path) {            List<string> sr = ftpHelper.GetDirctory(path);            foreach (var item in sr) {                TreeNode tchild = new TreeNode();                tchild.Text = item;                tchild.ImageIndex = 0;                LoadAll(tchild, path + item + "/");                tn.Nodes.Add(tchild);//把根节点加入到treeview的根节点            }        }        #endregion

5.窗体加载时初始化TreeView

    public Form2() {            InitializeComponent();            InitTree("/");        }
6.效果如图。

7.添加右键菜单contextMenuStrip1,和TreeView绑定


8.新建方法,由于涉及到新建文件夹重名的问题。因此我自己写了两个方法,递归,判断有没重名的,重名就加1

首先声明int 类型全局变量

 //新建文件夹时用到参数。        public static int newNode;


下面是点击右键菜单时新建方法

        //获得新建文件夹的名字        public void GetNewNodeName(string path) {            newNode = 0;            string checkpath = path.Replace("主目录", "");            checkpath = checkpath.Replace("\\", "/");            //先判断名字存在不存在,存在就递归加1            if (ftpHelper.CheckIsExisFolder(checkpath, "新建文件夹")) {                newNode++;                GetNodeNumber(checkpath, newNode);            } else {                newNode = 0;            }        }        public void GetNodeNumber(string path, int nodeNumber) {            string checkpath = path.Replace("主目录", "");            checkpath = checkpath.Replace("\\", "/");            if (ftpHelper.CheckIsExisFolder(checkpath, "新建文件夹" + nodeNumber) == true) {                newNode++;                GetNodeNumber(path, newNode);            }        }
效果如图


9.修改方法,点击节点,使得节点可以编辑。然后在节点编辑后触发方法,调用API

   private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {            treeView1.LabelEdit = true;            treeView1.SelectedNode.BeginEdit();        }

    private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {            string path = treeView1.SelectedNode.Parent.FullPath;            path = path.Replace("主目录", "");            path = path.Replace("\\", "/");            if (e.Label != null) {                
     ftpHelper.Rename(e.Node.Text, e.Label, path);
} }


这里需要注意一下,e.Label是老的 是新的值, e.Node.Text 是老的值。


10.删除文件夹,删除文件夹时,由于只能删除空文件夹,无法删除含有文件或者文件夹的文件夹。所以,我在删除时,先判断有无子文件或子文件夹在进行删除。

   private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {            string path = treeView1.SelectedNode.FullPath;            path = path.Replace("主目录", "");            path = path.Replace("\\", "/");            if (ftpHelper.delDir(path)) {                treeView1.SelectedNode.Remove();            } else {                MessageBox.Show("该文件夹不为空,无法删除");            }        }
当删除非空文件夹时,效果如图








1 0