EonaCatDeDupText/EonaCatDeDupText/Form1.cs

83 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace EonaCatDeDupText
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ProceedBtn_Click(object sender, EventArgs e)
{
if (!File.Exists(InputRTB.Text))
{
MessageBox.Show("Error, please insert a correct file path to remove duplicate lines.");
return;
}
else if (String.IsNullOrEmpty(OutputRTB.Text))
{
MessageBox.Show("Error, please insert a file path to save new file without duplicate lines.");
return;
}
try
{
string content = File.ReadAllText(InputRTB.Text);
List<string> repeatedList = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
List<string> UnrepeatedList = repeatedList.Distinct().ToList();
File.WriteAllLines(OutputRTB.Text, UnrepeatedList.ToArray());
}
catch (Exception exp)
{
MessageBox.Show("Error. " + exp.Message);
}
MessageBox.Show("File successfully created.");
}
private void InputBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
InputRTB.Text = openFileDialog1.FileName;
string ext = Path.GetExtension(InputRTB.Text);
OutputRTB.Text = openFileDialog1.FileName.Replace(ext, "_DeDuped" + ext);
}
}
private void OutputBrowse_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "All Files (*.*)|*.*";
DialogResult result = saveFileDialog1.ShowDialog();
saveFileDialog1.InitialDirectory = InputRTB.Text;
if (result == DialogResult.OK)
{
OutputRTB.Text = saveFileDialog1.FileName;
}
}
}
}