Does the .net framework have any builtin assemblies for handling reading and writing from and to text delimited files? Or is this something I have create myself?
A:
I think it is something you'll have to create yourself.
(I did anyway ;) ).
Frederik Gheysels
2009-03-19 15:50:17
+1
A:
No, it doesn't. You would have to write one yourself... or you could just use something someone else already made. Just google .net csv writer. Here is an example from google code: csv-reader-and-writer
EDIT: I have used the following one before and it worked well: (from knab.ws
public class CsvWriter
{
public static string WriteToString(DataTable table, bool header, bool quoteall)
{
StringWriter writer = new StringWriter();
WriteToStream(writer, table, header, quoteall);
return writer.ToString();
}
public static void WriteToStream(TextWriter stream, DataTable table, bool header, bool quoteall)
{
if (header)
{
for (int i = 0; i < table.Columns.Count; i++)
{
WriteItem(stream, table.Columns[i].Caption, quoteall);
if (i < table.Columns.Count - 1)
stream.Write(',');
else
stream.Write('\n');
}
}
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
WriteItem(stream, row[i], quoteall);
if (i < table.Columns.Count - 1)
stream.Write(',');
else
stream.Write('\n');
}
}
}
private static void WriteItem(TextWriter stream, object item, bool quoteall)
{
if (item == null)
return;
string s = item.ToString();
if (quoteall || s.IndexOfAny("\",\x0A\x0D".ToCharArray()) > -1)
stream.Write("\"" + s.Replace("\"", "\"\"") + "\"");
else
stream.Write(s);
}
}
jle
2009-03-19 15:51:16
+1
A:
There are no built in CSV readers in .NET for c#, however there are third party libraries available.
- FileHelpers is good for ORM type stuff but no good for arbitrary columns formats, like what Excel can deal with.
- CsvReader on CodeProject is not bad but this is missing some features such as handling fixed length fields and using strings as delimiters.
- Calcite-Csv Library on Google Code. This is my own effort which you might find useful.
Brendan
2009-03-19 15:54:17
+2
A:
There is actually a class called TextFieldParser in Microsoft.VisualBasic.dll that can read CSV files and the likes. I never tried it though.
Jakob Christensen
2009-03-19 15:56:19