前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 获取txt文件邮箱号码并去重复

C# 获取txt文件邮箱号码并去重复

作者头像
zls365
发布2020-08-19 14:35:00
9240
发布2020-08-19 14:35:00
举报
文章被收录于专栏:CSharp编程大全CSharp编程大全

这是咱们C# 开发交流群里好友昨天提的问题:

主要是从txt文件中删除重复的邮箱号

以下是实现的代码:

代码语言:javascript
复制
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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        string dataPath = @"C:\Users\admin\Desktop\111";
        public void FileReadWriter(string filePath)
        {
            if (Directory.Exists(filePath))

            {

                string[] filenames = Directory.GetFiles(filePath);

                foreach (string fname in filenames)
                {

                    FileInfo finfo = new FileInfo(fname);
                    List<string> posts = new List<string>();
                    if (finfo.Name.Substring(finfo.Name.Length - 4, 4) == ".txt")
                    {
                        foreach (string str in File.ReadAllLines(finfo.FullName, Encoding.Default))
                        {
                            posts.Add(str);
                        }
                        HashSet<string> hs = new HashSet<string>(posts);//此时已经去掉重复的数据保存在hashset中 方法1
                        posts = posts.Distinct().ToList();// 方法2
                        StreamWriter sw = new StreamWriter(finfo.FullName, false);
                        foreach (var post in posts) //方法1
                        {
                            sw.WriteLine(post);
                        }
                        //foreach (var post in hs)//方法2
                        //{
                        //    sw.WriteLine(post);
                        //}

                        sw.Close();
                    }


                }
            }

        }
        public void FileWriter(string filePath)
        {


        }

            private void button1_Click(object sender, EventArgs e)
        {
            FileReadWriter(dataPath);

            MessageBox.Show("文件去重完成");

        }
    }
}

运行效果:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-04-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档