winform:删除两文件下同名文件

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

namespace DeleSameInTwoFolder
{
    public partial class Form1 : Form
    {
        Dictionary<string, string> m_dic1Same = new Dictionary<string, string>();
        Dictionary<string, string> m_dic2Same = new Dictionary<string, string>();
        public Form1()
        {
            InitializeComponent();
        }

        //文件夹1
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog(); //是调用文件浏览器控件;           
            if (dr == System.Windows.Forms.DialogResult.OK) //是判断文件浏览器控件是否返回ok,即用户是否确定选择。如果确定选择,则弹出用户在文件浏览器中选择的路径:                
            {
                MessageBox.Show(folderBrowserDialog1.SelectedPath);
                label3.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        //文件夹2
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog2.ShowDialog(); //是调用文件浏览器控件;           
            if (dr == System.Windows.Forms.DialogResult.OK) //是判断文件浏览器控件是否返回ok,即用户是否确定选择。如果确定选择,则弹出用户在文件浏览器中选择的路径:                
            {
                MessageBox.Show(folderBrowserDialog2.SelectedPath);
                label4.Text = folderBrowserDialog2.SelectedPath;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        //比较同名文件
        private void button3_Click(object sender, EventArgs e)
        {
            if (label3.Text != "" && label4.Text != "")
            {
                var dir1 = label3.Text;
                Dictionary<string, string> dic1All = new Dictionary<string, string>();
                foreach (var f in new DirectoryInfo(dir1).GetFiles("*.*", SearchOption.AllDirectories))
                {
                    FileInfo info = f;
                    dic1All[info.FullName] = info.Name;
                }

                var dir2 = label4.Text;
                Dictionary<string, string> dic2All = new Dictionary<string, string>();
                foreach (var f in new DirectoryInfo(dir2).GetFiles("*.*", SearchOption.AllDirectories))
                {
                    FileInfo info = f;
                    dic2All[info.FullName] = info.Name;
                }

                m_dic1Same.Clear();
                m_dic2Same.Clear();
                foreach (var fold1 in dic1All)
                {
                    foreach (var fold2 in dic2All)
                    {
                        if (fold1.Value == fold2.Value)
                        {
                            m_dic1Same[fold1.Key] = fold1.Value;
                            m_dic2Same[fold2.Key] = fold2.Value;
                        }
                    }
                }
                listBox1.Items.Clear();
                foreach (var item in m_dic1Same)
                {
                    listBox1.Items.Add(item.Value);
                }

                
            }
            else
            {
                MessageBox.Show("请选择两个文件夹");
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (var fold in m_dic1Same)
            {
                File.Delete(fold.Key);
            }
            m_dic1Same.Clear();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            foreach (var fold in m_dic2Same)
            {
                File.Delete(fold.Key);
            }
            m_dic2Same.Clear();
        }

    }
}

在这里插入图片描述
源码
https://github.com/luoyikun/DeleSameInTwoFolder

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值