Create CSV file with post build event C#

169 Views Asked by At

I have some dictionary:

Dictionary<int, string> myDict = new Dictionary<int, string>;

Dictionary holds: 1,mike 2,john ....

I want to create a CSV file from the dictionary with post build event but also ignore the file in repo.

I use the following to create the file:

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

Can I do this with post build event or other way? Thanks

1

There are 1 best solutions below

0
Y.Hoegger On

I would look into Roslyn source generators. With this you would generate a file at compile time, so the generated file is not in your repository but available at run time