遍历文件夹以及复制文件夹

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ThumbnailImages
{
    public partial class ImageList : Form
    {
        public ImageList()
        {
            InitializeComponent();
        }
        private static string _source, _target;
        private void ImageList_Load(object sender, EventArgs e)
        { }
        public void ListFiles(FileSystemInfo info)
        {
            if (!info.Exists) return;
            DirectoryInfo dir = info as DirectoryInfo;
            if (dir == null) return;
            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                if (file != null)
                {
                    if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg" || file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".JPG" || file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".bmp" || file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".BMP")
                    {
                        this.treeView1.Nodes.Add(file.FullName);
                    }
                }
                else
                {
                    try
                    {
                        this.treeView1.Nodes.Add(dir.FullName);
                        ListFiles(files[i]);
                    }
                    catch (Exception)
                    { }
                }
            }
        }
        public void ListFiles(FileSystemInfo info,bool flag)
        {
            if (!info.Exists) return;
            DirectoryInfo dir = info as DirectoryInfo;
            if (dir == null) return;
            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                if (file != null)
                {
                    if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg" || file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".JPG" || file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".bmp" || file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".BMP")
                    {
                        this.treeView2.Nodes.Add(file.FullName);
                    }
                }
                else
                {
                    try
                    {
                        this.treeView2.Nodes.Add(dir.FullName);
                        ListFiles(files[i],false);
                    }
                    catch (Exception)
                    { }
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                ListFiles(new DirectoryInfo(this.folderBrowserDialog1.SelectedPath));
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txt_source.Text = this.folderBrowserDialog1.SelectedPath;
                _source = this.folderBrowserDialog1.SelectedPath;
                ListFiles(new DirectoryInfo(_source));
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txt_target.Text = this.folderBrowserDialog1.SelectedPath;
                _target = this.folderBrowserDialog1.SelectedPath;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (this.txt_source.Text.Trim() == "")
            {
                MessageBox.Show("请选择要复制的目标文件夹!");
                return;
            }
            if (this.txt_target.Text.Trim() == "")
            {
                MessageBox.Show("请选择目的文件夹!");
                return;
            }
            if (this.txt_source.Text == this.txt_target.Text)
            {
                MessageBox.Show("目标文件夹与目的文件夹不能相同");
            }
            CopyFiles(_source, _target);
            ListFiles(new DirectoryInfo(_target),false);
        }
        private void CopyFiles(string varFromDirectory, string varToDirectory)
        {
            Directory.CreateDirectory(varToDirectory);
            if (!Directory.Exists(varFromDirectory)) return;
            string[] directories = Directory.GetDirectories(varFromDirectory);
            if (directories.Length > 0)
            {
                foreach (string d in directories)
                {
                    CopyFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));
                }
            }
            string[] files = Directory.GetFiles(varFromDirectory);
            if (files.Length > 0)
            {
                foreach (string s in files)
                {
                    File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf(" \\")),true);
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值