namespace ConsoleApplication4 { class Program { static string path = @"C:\Users\鸣\Desktop\1111.csv"; static void Main(string[] args) {
if (File.Exists(path)) { File.Delete(path); GenerateFIle(); } List<string[]> content = new List<string[]>();
ReadFile(content); int count = 0; foreach (var line in content) { count++; Console.WriteLine(line[0]+','+line[1]+"count:"+count); } Console.ReadKey();
}
private static void ReadFile(List<string[]> content) { using (StreamReader sr = new StreamReader(path)) { while (true) { var line = sr.ReadLine(); if (line == null) break; string[] words = line.Split(','); List<string> firstline = content.Select(p => p[0]).ToList(); if (!firstline.Contains(words[0])) { content.Add(words); } } } }
private static void GenerateFIle() { using (StreamWriter sw = new StreamWriter(path)) { var count = 0; Random rnd = new Random(); for (var i = 0; i < 100; i++) { count++; sw.WriteLine(count + "," + rnd.Next(1, 100)); } count = 0; for (var i = 0; i < 100; i++) { count++; sw.WriteLine(count + "," + rnd.Next(1, 100)); } } }