C# Dictionary to .csv

Maybe the easiest:

String csv = String.Join(
    Environment.NewLine,
    data.Select(d => $"{d.Key};{d.Value};")
);
System.IO.File.WriteAllText(pathToCsv, csv);

You'll need to add using LINQ and use at least .NET 3.5


Try the following

using (var writer = new StreamWriter(@"the\path\to\my.csv")) {
  foreach (var pair in data) {
    writer.WriteLine("{0};{1};", pair.Key, pair.Value);
  }
}

Note: This will fail to work if the key or value elements can contain a ;. If so you will need to add an escaping mechanism to handle that


  File.WriteAllLines(@"pathtocsv.csv", data.Select(x => x.Key + ";" + x.Value + ";")));

Tags:

C#

Csv

File