Saving from List to txt

后端 未结 7 1808
生来不讨喜
生来不讨喜 2020-12-03 13:26

I want my program to read from two text files into one List. The List is sorting and cleaning duplicates.

I want the

7条回答
  •  眼角桃花
    2020-12-03 14:07

    There's a handy little method File.WriteAllLines -- no need to open a StreamWriter yourself:

    In .net 4:

    File.WriteAllLines(speichern, ausgabeListe);
    

    In .net 3.5:

    File.WriteAllLines(speichern, ausgabeListe.ToArray());
    

    Likewise, you could replace your reading logic with File.ReadAllLines, which returns an array of strings (use ToList() on that if you want a List).

    So, in fact, your complete code could be reduced to:

    // Input
    List data = File.ReadAllLines(pfad + datei)
        .Concat(File.ReadAllLines(pfad2 + datei2))
        .Distinct().ToList();
    
    // Processing
    data.Sort(); 
    
    // Output
    data.ForEach(Console.WriteLine); 
    File.WriteAllLines(speichern, data);
    

提交回复
热议问题