本文整理汇总了C#中CsvWriter.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# CsvWriter.Flush方法的具体用法?C# CsvWriter.Flush怎么用?C# CsvWriter.Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CsvWriter
的用法示例。
在下文中一共展示了CsvWriter.Flush方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Flush
public void Flush()
{
using (var stream = new MemoryStream())
using (var writer = new CsvWriter(stream))
{
string actual = String.Empty;
writer.Flush();
actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual("", actual);
writer.WriteField("Field1");
actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual("", actual);
writer.Flush();
actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual("Field1", actual);
}
}
示例2: copy_to_copies_records
public void copy_to_copies_records()
{
var csv = @"First,Second
1,2
3,4
5,6";
using (var reader = CsvReader.FromCsvString(csv))
using (var stringWriter = new StringWriter())
using (var writer = new CsvWriter(stringWriter))
{
writer.NewLine = "<EOL>";
Assert.Equal(4, reader.CopyTo(writer));
writer.Flush();
Assert.Equal("First,Second<EOL>1,2<EOL>3,4<EOL>5,6<EOL>", stringWriter.ToString());
}
}
示例3: GetLinesFromCsv
// extract lines from CSV
private static IList<string> GetLinesFromCsv(string csv)
{
var lines = new List<string>();
using (var csvReader = CsvReader.FromCsvString(csv))
{
while (csvReader.HasMoreRecords)
{
var record = csvReader.ReadDataRecord();
using (var stringWriter = new StringWriter())
using (var csvWriter = new CsvWriter(stringWriter))
{
csvWriter.WriteRecord(record);
csvWriter.Flush();
lines.Add(stringWriter.ToString());
}
}
}
return lines;
}
示例4: copy_to_works_with_large_input
public void copy_to_works_with_large_input()
{
var csv = string.Empty;
using (var stringWriter = new StringWriter())
using (var writer = new CsvWriter(stringWriter))
{
for (var i = 0; i < 1000; ++i)
{
writer.WriteRecord("value0" + i, "value1" + i, "value2" + i);
}
writer.Flush();
csv = stringWriter.ToString();
}
using (var reader = CsvReader.FromCsvString(csv))
using (var writer = new CsvWriter(new StringWriter()))
{
writer.NewLine = "<EOL>";
Assert.Equal(1000, reader.CopyTo(writer));
}
}
示例5: WriteScreenInformationToCSV
private static void WriteScreenInformationToCSV()
{
#region WriteScreenInformationToCSV
using (var stringWriter = new StringWriter())
using (var writer = new CsvWriter(stringWriter))
{
Screen.AllScreens.WriteCsv(writer);
writer.Flush();
Console.WriteLine(stringWriter);
}
#endregion
}
示例6: WriteField_EscapedWhiteSpace
public void WriteField_EscapedWhiteSpace()
{
using (var stream = new MemoryStream())
using (var writer = new CsvWriter(stream))
{
string actual = null;
writer.WriteField(" Field1");
writer.WriteField("Field2 ");
writer.WriteField("Field3\n");
writer.WriteField("\nField4");
writer.Flush();
actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual("\" Field1\",\"Field2 \",\"Field3\n\",\"\nField4\"", actual);
writer.WriteField("Field5\t");
writer.WriteField("\tField6");
writer.WriteField("Field7"); // non-escaped
writer.Flush();
actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual("\" Field1\",\"Field2 \",\"Field3\n\",\"\nField4\",\"Field5\t\",\"\tField6\",Field7", actual);
}
}
示例7: WriteRecord_Simple
public void WriteRecord_Simple()
{
using (var stream = new MemoryStream())
using (var writer = new CsvWriter(stream))
{
writer.WriteRecord("Field1", "Field2", "Field3", "Field4");
writer.Flush();
Assert.AreEqual(0, writer.CurrentField);
Assert.AreEqual(1, writer.CurrentRecord);
var actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual("Field1,Field2,Field3,Field4", actual);
}
}
示例8: WriteField_TypeOverloads
public void WriteField_TypeOverloads()
{
using (var stream = new MemoryStream())
using (var writer = new CsvWriter(stream))
{
char[] chArray = "Field".ToCharArray();
object obj = new object();
writer.WriteField(true);
writer.WriteField(false);
writer.WriteField('c');
writer.WriteField(chArray);
writer.WriteField(chArray, 0, chArray.Length - 2);
writer.WriteField((int)0);
writer.WriteField((int)Int32.MaxValue);
writer.WriteField((int)Int32.MinValue);
writer.WriteField((uint)0);
writer.WriteField((uint)UInt32.MaxValue);
writer.WriteField((uint)UInt32.MinValue);
writer.WriteField((long)0);
writer.WriteField((long)Int64.MaxValue);
writer.WriteField((long)Int64.MinValue);
writer.WriteField((ulong)0);
writer.WriteField((ulong)UInt64.MaxValue);
writer.WriteField((ulong)UInt64.MinValue);
writer.WriteField((float)3.14f);
writer.WriteField((double)3.1415);
writer.WriteField((decimal)3.1415926m);
writer.WriteField(obj);
writer.Flush();
var expected = String.Format(
"{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20}",
Boolean.TrueString,
Boolean.FalseString,
'c', new string(chArray),
new string(chArray, 0, chArray.Length - 2),
((int)0).ToString(writer.FormatProvider),
Int32.MaxValue.ToString(writer.FormatProvider),
Int32.MinValue.ToString(writer.FormatProvider),
((uint)0).ToString(writer.FormatProvider),
UInt32.MaxValue.ToString(writer.FormatProvider),
UInt32.MinValue.ToString(writer.FormatProvider),
((long)0).ToString(writer.FormatProvider),
Int64.MaxValue.ToString(writer.FormatProvider),
Int64.MinValue.ToString(writer.FormatProvider),
((ulong)0).ToString(writer.FormatProvider),
UInt64.MaxValue.ToString(writer.FormatProvider),
UInt64.MinValue.ToString(writer.FormatProvider),
((float)3.14f).ToString(writer.FormatProvider),
((double)3.1415).ToString(writer.FormatProvider),
((decimal)3.1415926m).ToString(writer.FormatProvider),
obj.ToString());
var actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual(expected, actual);
}
}
示例9: WriteField_TrackingCurrentFieldAndRecord
public void WriteField_TrackingCurrentFieldAndRecord()
{
using (var stream = new MemoryStream())
using (var writer = new CsvWriter(stream))
{
var builder = new StringBuilder();
for (int i = 0; i < 10; ++i)
{
Assert.AreEqual(i, writer.CurrentRecord);
for (int j = 0; j < 5; ++j)
{
Assert.AreEqual(j, writer.CurrentField);
var s = String.Format("Field({0}:{1})", writer.CurrentRecord, writer.CurrentField);
builder.Append(s + ",");
writer.WriteField(s);
}
builder.Append(Environment.NewLine);
writer.WriteRecord();
}
writer.Flush();
var expected = builder.ToString()
.Replace("," + Environment.NewLine, Environment.NewLine)
.TrimEnd(Environment.NewLine.ToCharArray());
var actual = writer.Encoding.GetString(stream.ToArray());
Assert.AreEqual(expected, actual);
}
}
示例10: flush_calls_flush_on_underlying_text_writer
public void flush_calls_flush_on_underlying_text_writer()
{
var textWriter = new Mock<TextWriter>();
textWriter.Setup(x => x.Flush()).Verifiable();
using (var writer = new CsvWriter(textWriter.Object))
{
writer.Flush();
}
textWriter.Verify();
}
示例11: flush_throws_if_disposed
public void flush_throws_if_disposed()
{
var writer = new CsvWriter(new MemoryStream());
writer.Dispose();
Assert.Throws<ObjectDisposedException>(() => writer.Flush());
}