Flat files in my terms :-
One row in a file = 1 datarow
Columns are separated by a delimiter and every row should have the same number of columns and the first row may or may not be column headings.
You can certainly read a file line by line and load into a datatable for instance.
There are classes to assist. File.Open can open a file for reading. A TextReader can read the lines and you can split the line into a string array.
There's alot to learn there but it's well worth it you'll use it again.
I generally use a StreamReader to read files and would need to probably look at a quick example for a refresher.
Certainly what you need can be achieved and it's actually not that difficult.
You could google for some good examples i'm sure of that. It's where I found the info originally.
You would probably want to create a class that has the methods for reading and writing to the file and a method for getting the file read into a datatable for you to use. If you make it comma separated values with double quotes around each column value you can also open it in Excel with no changes.
If you hae lots of columns of different types you may need to do alot of casting yourself which is not very pretty but again not too hard either.
You may find this useful. Even if you don't want to convert it into Xml, I'm sure the code sample will provide the code you need to read the text file and split it up into fields.
However I think this would be a better approach. It's ready-made and allows you to connect to the flat file in the same way you'd connect to a normal database.
This only helps with reading the data, not saving any changes back to the file. Do you need to do this?
Another option is to store the data as Xml. You can still store it in a single file, but you don't have the limitations of a flat file. You can have a proper data structure if you want, and .Net has plenty of built-in ways for you to load, manipulate and save this data.
Sean
This is the nature of a flat file system. It's essentially a passive sytem and there is no built-in mechanism to manage multi-user access.
If multi-user access is a requirement I think you really need to be looking at a server or file based system that can manage this for you. For a flat file system you would pretty much need to write your own database manager.
Hi Navaneeth,
Please look into this Code for flat file Writing & Reading using C#
======================Writing a Flat File (.TXT) File ===============================
using System;
using System.IO;
namespace MyTextExample
{
class MyTextClass
{
static void main(string[] args)
{
TextWriter tw = new StreamWriter("date.txt");
tw.WriteLine(DateTime.Now);
tw.close();
}
}
}
================== Reading from a Flat File =======================================
using System;
using System.IO;
namespace MyTextReader
{
class MyTextReaderClass
{
static void main(string[] args)
{
TextReader tr = new StreamReader("date.txt");
Console.WriteLine(tr.ReadLine());
tr.Close();
}
}
}
If you need more help about flat file generation... Please reply ...
Regards