递归拷贝文件 用于自动更新的update 程序中

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace WindowsFormsApplication24
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TreeNode tn = treeView1.Nodes.Add("zwj");
            string path = textBox1.Text;// @"C:\Users\Administrator\Desktop\code";
            SearchFile(path, tn,@"aa\");


        }
        /// <summary>
        /// 文件夹拷贝
        /// </summary>
        /// <param name="v">拷贝的来源</param>
        /// <param name="tn">绑定到TreeNode</param>
        /// <param name="path">设置根目录路径</param>
        private void SearchFile(string v, TreeNode tn,string path)
        {
            string[] dires = Directory.GetDirectories(v,"*.*");
            string[] files = Directory.GetFiles(v, "*.*");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            foreach (var item in files)
            {
                tn.Nodes.Add(item.ToString());
                File.Copy(item, path + @"\"+Path.GetFileName(item), true);
            }

            foreach (var item in dires)
            {
                TreeNode tsub = tn.Nodes.Add(item);
                string p1 = path + item.Replace(v, "");
                if (!Directory.Exists( p1))
                {
                    //string str = Directory.GetParent(item).ToString();
                    Directory.CreateDirectory(  p1);
                }
                SearchFile(item, tsub,p1);// 递归调用
            }
        }

    }
}

  

posted @ 2016-09-22 10:08  刀小爱  阅读(169)  评论(0编辑  收藏  举报