Kajabity.Tools.Csv.CsvWriter.WriteRecord C# (CSharp) Метод

WriteRecord() публичный Метод

Writes a single record to the output stream appending a new line to the end of the previous line if this is not the first record.
public WriteRecord ( string record ) : void
record string The record to be written - an array of string fields.
Результат void
        public void WriteRecord(string[] record)
        {
            flush++;
            if (record != null && record.Length > 0)
            {
                if (recordCount++ > 0)
                {
                    writer.Write(newLine);
                    fieldCount = 0;
                }

                foreach (string field in record)
                {
                    WriteField(field);
                }

                if (--flush == 0)
                {
                    writer.Flush();
                }
            }
        }

Usage Example

Пример #1
0
        public void TestWriteRecord()
        {
            string filename = Path.Combine(CsvOutputDirectory, "test-write-record.csv");
            string[] record = new string[] { "AAAA", "BBBB", "CCCC" };
            const int lenRecord = 14; // Strings, commas.

            Stream stream = null;
            try
            {
                //	Create the temp file (or overwrite if already there).
                stream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                stream.SetLength(0);
                stream.Close();

                //	Check it's empty.
                FileInfo info = new FileInfo(filename);
                Assert.AreEqual(0, info.Length, "File length not zero.");

                //  Open for append
                stream = File.OpenWrite(filename);

                //	Append a record.
                CsvWriter writer = new CsvWriter(stream);
                writer.WriteRecord(record);
                stream.Flush();
                stream.Close();

                //	Check it's not empty.
                info = new FileInfo(filename);
                Assert.AreEqual(lenRecord, info.Length, "File length not increased.");
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    //File.Delete( filename );  // Keep it for debugging.
                }
            }
        }
All Usage Examples Of Kajabity.Tools.Csv.CsvWriter::WriteRecord