blob: c709017df812d780267c9fc39f0728195e649f6a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# Appending to an Existing CSV File
###### Example
```cs
void Main()
{
var records = new List<Foo>
{
new Foo { Id = 1, Name = "one" },
};
// Write to a file.
using (var writer = new StreamWriter("path\\to\\file.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(records);
}
records = new List<Foo>
{
new Foo { Id = 2, Name = "two" },
};
// Append to the file.
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
// Don't write the header again.
HasHeaderRecord = false,
};
using (var stream = File.Open("path\\to\\file.csv", FileMode.Append))
using (var writer = new StreamWriter(stream))
using (var csv = new CsvWriter(writer, config))
{
csv.WriteRecords(records);
}
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
```
###### Output
```
Id,Name
1,one
2,two
```
|