Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi all
I have a text file like this
SQL
StartTime           EndTime              Level     Reason      Status
20130120114131      20130120114222       2         03          250
20130120114131      20130120114223       1         05          071
20130120114131      20130120114222       3         06          2695

how can I read this file and use in my application ?
please help me .
Posted
Updated 29-Nov-13 20:59pm
v2
Comments
phil.o 30-Nov-13 5:20am    
What have you tried so far?
Where are you stuck?

That really depends on more than a quick look at a formatted text view - how you read it will depend on the actual file content.

There are a huge number of ways you could read this data: if it is fixed columns, with spaces between them, the you could read each line of the file and use string.Substring to extract each data section, then use the various TryParse and TryParseExact methods to convert them to teh appropriate datatypes. Be wary though of the "Level" field - you need to know if numbers do this:
 2
 3
10
Or this:
2
 3
 10
before you start splitting data up.

If it is tab separated, then you could use a CSV reader, probably with TryParseExact to split out the dates.

Look at the file using a hex reader as work out exactly what you have first.
 
Share this answer
 
You may try out the following code that ports data from text file to a datagridview. It is up to you to adapt it to suit your need. Also take note of what OriginalGriff has said.

I am using your text file as an example and named it as sample.txt and place it on c drive.


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();

            // put each line of the text file into a string array. 
            string[] lines = System.IO.File.ReadAllLines(@"c:\sample.txt");

            // loop thru each line
            for (int i = 0; i < lines.Length; i++)
            {
                DataRow newrow = dt.NewRow();

                // split each line into words by spaces
                string[] words = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                // if i = 0, means this is the header row
                if (i == 0)
                {
                    for (int j = 0; j < words.Length; j++)
                    {
                        // add column
                        dt.Columns.Add(words[j]);

                    }
                }
                else
                {
                    for (int j = 0; j < words.Length; j++)
                    {
                        // fill cell content
                        newrow[dt.Columns[j]] = words[j];
                    }

                    // add new row
                    dt.Rows.Add(newrow);
                }
            }

            dataGridView1.DataSource = dt;
        }
    }
}
 
Share this answer
 
v3
 
Share this answer
 
v2
to read file write
string Data = File.ReadAllText(@"Path");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900