30

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

I want the List<T> to save (after sorting and cleaning) to a txt file.

But when I looked in the result txt file, I found this message:

System.Collections.Generic.List`1[System.String]

Does anyone have an idea how I could fix this error?

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Uniqpass
{
    class Program
    {
        static void Main(string[] args)
        {

            String pfad = "C:\\Dokumente und Einstellungen\\Bektas\\Desktop\\test\\";
            String pfad2 = "C:\\Dokumente und Einstellungen\\Bektas\\Desktop\\test\\";
            String speichern = "C:\\Dokumente und Einstellungen\\Bektas\\Desktop\\test\\ausgabe.txt";
            String datei = "text1.txt";
            String datei2 = "text2.txt";

            try
            {

                //Einlesen TxT 1
                List<String> pass1 = new List<String>();
                StreamReader sr1 = new StreamReader(pfad + datei);
                while (sr1.Peek() > -1)
                {

                    pass1.Add(sr1.ReadLine());
                }
                sr1.Close();
                //Einlesen TxT 2
                StreamReader sr2 = new StreamReader(pfad2 + datei2);
                while (sr2.Peek() > -1)
                {
                    pass1.Add(sr2.ReadLine());
                }
                sr2.Close();

                List<String> ausgabeListe = pass1.Distinct().ToList();
                ausgabeListe.Sort();

                ausgabeListe.ForEach(Console.WriteLine);

                StreamWriter file = new System.IO.StreamWriter(speichern);
                file.WriteLine(ausgabeListe);
                file.Close();


            }
            catch (Exception)
            {
                Console.WriteLine("Error");
            }

            Console.ReadKey();
        }
    }
}
casperOne
  • 73,706
  • 19
  • 184
  • 253
Jon Cylo
  • 311
  • 1
  • 3
  • 4

7 Answers7

74

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<string>).

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

// Input
List<String> data = File.ReadAllLines(pfad + datei)
    .Concat(File.ReadAllLines(pfad2 + datei2))
    .Distinct().ToList();

// Processing
data.Sort(); 

// Output
data.ForEach(Console.WriteLine); 
File.WriteAllLines(speichern, data);
Heinzi
  • 167,459
  • 57
  • 363
  • 519
10

It's this line which writes the ToString representation of the List, resulting into the text line you got:

StreamWriter file = new System.IO.StreamWriter(speichern);
file.WriteLine(ausgabeListe);
file.Close();

Instead you want to write each line.

StreamWriter file = new System.IO.StreamWriter(speichern);
ausgabeListe.ForEach(file.WriteLine);
file.Close();
Matten
  • 17,365
  • 2
  • 42
  • 64
1

Try the code below:

StreamWriter writer = new StreamWriter("C:\\Users\\Alchemy\\Desktop\\c#\\InputFileFrmUser.csv");
list = new List<Product>() { new Product() { ProductId=1, Name="Nike 12N0",Brand="Nike",Price=12000,Quantity=50},
        new Product() { ProductId =2, Name = "Puma 560K", Brand = "Puma", Price = 120000, Quantity = 55 },
        new Product() { ProductId=3, Name="WoodLand V2",Brand="WoodLand",Price=21020,Quantity=25},
        new Product() { ProductId=4, Name="Adidas S52",Brand="Adidas",Price=20000,Quantity=35},
        new Product() { ProductId=5, Name="Rebook SPEED2O",Brand="Rebook",Price=1200,Quantity=15}};

foreach (var x in list) {
    string wr = x.ProductId + " " + x.Name + "" + x.Brand + " " + x.Quantity + " " + x.Price;
    writer.Flush();
    writer.WriteLine(wr);

}
Console.WriteLine("--------ProductList Updated SucessFully----------------");
ivcubr
  • 1,988
  • 9
  • 20
  • 28
1

Loop through the list, writing each line individually:

StreamWriter file = new System.IO.StreamWriter(speichern);
foreach(string line in ausgabeListe)
    file.WriteLine(line);
file.Close();
itsme86
  • 19,266
  • 4
  • 41
  • 57
0

I am using the LINQ like below to write each line to text file.

var myList=new List<string>
{
    "Hello",
    "World"
};
using (var file = new StreamWriter("myfile.txt"))
{
    myList.ForEach(v=>file.WriteLine(v));
}
LENG UNG
  • 473
  • 6
  • 5
0

You are writing the list object into the file, so you see the type name.

Just as you are using ForEach to write the contents to the Console, you need to iterate over ausgabeListe, calling WriteLine() for each item in the list.

Jay
  • 56,361
  • 10
  • 99
  • 123
0
StreamWriter file = new System.IO.StreamWriter(speichern);
foreach(string x in ausgabeListe)
    file.WriteLine(x);
file.Close();
watbywbarif
  • 6,487
  • 8
  • 50
  • 64
  • Could you name the variables, such as "speichern" and "ausgabeListe" with something easier to understand? – Weifeng Oct 19 '16 at 06:06