|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
8250
|
|
Downloads :
|
345
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
This article describes delimited text files of data and in particular Comma-Separated Values (CSV) files. The accompanying sample program reads CSV files into a DataGridView, allows editing of the data, and saves the data back to the CSV file.
Delimited files have been used for many years to store data. In their simplest form, delimited files consist of records with a fixed number of fields in each record of the file. In Windows, records are ended by a carriage-return and line-feed (vbCrLf in VB) which can be coded in C# as "\r\n". Any delimiter can be used to separate fields; the most common delimiters are commas and tabs. When commas are used, the format is usually called Comma-Separated Values (CSV). The CSV format is extremely common but it has never been standardized. See Common Format and MIME Type for Comma-Separated Values (CSV) Files for a standard that applies to the internet, but for the internet, XML files are usually used instead of CSV files. CSV files are often considered to be Excel files, since Excel imports and exports data in CSV format but many other programs use CSV and CSV was used extensively before Excel was developed.
There are many tools for processing delimited text files. Many programs can import and export them. Older Microsoft database software reads and writes CSV files similar to reading and writing tables, but Microsoft is dropping support of the older database technologies. It is entirely possible to read and write delimited text files using plain File IO classes such as StreamReader and StreamWriter.
The CSV format has traditionally been directly supported by the Basic language, including Visual Basic. Delimited text files are not directly supported by .Net but the TextFieldParser class in the Microsoft.VisualBasic.FileIO namespace can read delimited text files and can be used by C# programs.
Microsoft.VisualBasic.FileIO namespace
The Microsoft.VisualBasic.FileIO namespace is not part of .Net; it is part of Visual Basic but it is available for use by C# programs too. There are many useful classes in it, including the TextFieldParser class. The TextFieldParser class can read most delimited text files, but it does not write them. Writing the data is easier than reading since reading the data requires a bit of parsing. The parsing is often trivial and when it is, it would be more efficient to just do the code in C#. If your data is more complex or if you want to just let the TextFieldParser class do it, the TextFieldParser class is easy to use. Note that since CSV is not a standard, you might encounter data that is not compatible with the Microsoft software.
To use the TextFieldParser class, be sure to add a reference to Microsoft.VisualBasic. Create an instance of the class then set the delimiters using SetDelimiters, as in:
Parser = new TextFieldParser(Filename); Parser.SetDelimiters(","); Then read records as in: String[] Fields = null; Fields = Parser.ReadFields();
Note that if the first record is a header record with field names, then you need to process it separately. The ReadFields method reads the fields of a record into a String array. Read the fields using ReadFields until EndOfData is true.
Sample Program
The sample program reads CSV files into a DataGridView, allows editing of the data, and saves the data back to the CSV file. The sample is not intended to be a complete application, but it can help get you started. The sample program writes the data using StreamWriter. All fields are written enclosed in double-quotes. You need to modify the code to satisfy your requirements.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Sam Hobbs
Programmer / Analyst initially for the Unites States Army in 1972. Subsequent employers and clients have included Carte Blanche, Bank of America, Parsons Corporation, Lockheed Corporation and SHL Systemhouse.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|