One way to accomplish this is to read the existing file into a
HashSet
, and then attempt to add new entries to the
HashSet
.
The nature of a
HashSet
is to disallow duplicate values. Fortunately, when you try to add a duplicate entry, no exception is thrown, so there's no special handling necessary.
I wrote code to test this theory (took about 30 minutes), and it works fine.
This smells like a homework assignment (nobody in the real world enterprise environment would save a lot of data in a text file on the file system), so I'm not going to just hand over the actual code.
EDIT ====================================
I honestly don't believe you when you say this isn't a homework assignment, and think you should try to figure it out with just the hint I gave you. Truthfully, any hobby programmer worth his salt would have done that already. How else are you going to learn this stuff if you beg for code instead of researching and figuring it out on your own? You simply have to do the work.
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApp4
{
class Program
{
private static HashSet<string> entries = new HashSet<string>();
private static string fileName = Path.Combine(Directory.GetCurrentDirectory(), "Test.txt");
private static bool loadingFile = false;
static void Main(string[] args)
{
GetEntries();
DataGridViewRowList dataGridView1 = new DataGridViewRowList();
string entry = string.Empty;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string value = row.Cells[4].Value as string;
if (!string.IsNullOrEmpty(value))
{
entry = string.Format(@"{0}\t{1}\t{2}{3}",
row.Cells[1].Value,
row.Cells[2].Value,
row.Cells[4].Value,
Environment.NewLine);
AddEntry(entry);
}
}
}
static private void GetEntries()
{
loadingFile = true;
if (File.Exists(fileName))
{
foreach(string line in File.ReadLines(fileName))
{
AddEntry(line.Trim());
}
}
loadingFile = false;
}
static private void AddEntry(string entry)
{
if (entries.Add(entry))
{
SaveEntry(entry);
}
}
static private void SaveEntry(string value)
{
if (!loadingFile)
{
File.AppendAllText(fileName, value);
}
}
}
public class DataCell
{
public object Value { get; set; }
}
public class DataGridViewRow
{
public List<DataCell> Cells { get; set; }
public DataGridViewRow(string c0, string c1, string c2, string c3, string c4)
{
this.Cells = new List<DataCell>();
this.Cells.Add(new DataCell() { Value = c0 });
this.Cells.Add(new DataCell() { Value = c1 });
this.Cells.Add(new DataCell() { Value = c2 });
this.Cells.Add(new DataCell() { Value = c3 });
this.Cells.Add(new DataCell() { Value = c4 });
}
}
public class DataGridViewRowList : List<DataGridViewRow>
{
public DataGridViewRowList Rows { get { return this; } }
public DataGridViewRowList()
{
this.Add(new DataGridViewRow("cell0", "cell1", "cell2", "cell3", "cell4"));
this.Add(new DataGridViewRow("cell0A", "cell1A", "cell2A", "cell3A", "cell4A"));
this.Add(new DataGridViewRow("cell0B", "cell1B", "cell2B", "cell3B", "cell4B"));
this.Add(new DataGridViewRow("cell0C", "cell1C", "cell2C", "cell3C", "cell4C"));
this.Add(new DataGridViewRow("cell0D", "cell1D", "cell2D", "cell3D", "cell4D"));
this.Add(new DataGridViewRow("cell0E", "cell1E", "cell2E", "cell3E", ""));
this.Add(new DataGridViewRow("cell0D", "cell1D", "cell2D", "cell3D", "cell4D"));
this.Add(new DataGridViewRow("cell0D", "cell1D", "cell2D", "cell3D", "cell4E"));
}
}
}