diff options
author | chai <215380520@qq.com> | 2023-05-12 09:24:40 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-12 09:24:40 +0800 |
commit | 2a1cd4fda8a4a8e649910d16b4dfa1ce7ae63543 (patch) | |
tree | a471fafed72e80b4ac3ac3002e06c34220dd6058 /ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/writing/appending-to-an-existing-file/index.md | |
parent | b8a694746562b37dc8dc5b8b5aec8612bb0964fc (diff) |
*misc
Diffstat (limited to 'ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/writing/appending-to-an-existing-file/index.md')
-rw-r--r-- | ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/writing/appending-to-an-existing-file/index.md | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/writing/appending-to-an-existing-file/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/writing/appending-to-an-existing-file/index.md new file mode 100644 index 0000000..c709017 --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/writing/appending-to-an-existing-file/index.md @@ -0,0 +1,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 +``` |