Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code:
C#
string filename = @"E:\DvrAlarmLog.txt";

string str = string.Empty;

if (File.Exists(filename))
{
    string[] arrays = new string[7];
    StreamReader sr = new StreamReader(filename);
    String line = string.Empty;

    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID"), new DataColumn("Start Date"), new DataColumn("End Date") });

    StringBuilder sb = new StringBuilder();

    List<string> list = new List<string>();

    using (StreamReader srr = new StreamReader(filename))
    {
        //string[] lines = System.IO.File.ReadAllLines(filename);
        //foreach (string linesingle in lines)
        //{
        //    if (linesingle.Equals("Alarm ended"))
        //    {
                while (srr.Peek() >= 0)
                {
                    list.Add(srr.ReadLine());
                }

            //    }
            //}
        }
       
    
    for (int i = 13; i < list.Count; i++)
    {
        string[] strlist = list[i].Split('.',':');

        foreach (string strdt in strlist)
        {
            if (!strdt.Contains("-----------------------------------------------------"))
            {
                dt.Rows.Add(strlist[0], strlist[1], strlist[2]);
            }
        }
    }
      GridView1.DataSource = dt;
      GridView1.DataBind();            
}

and my textfile is this how split data
-------------------------------------------------
| DVR alarm log. Start time: 26-11-2015 21:02:36 |
-------------------------------------------------

Alarm started. Guid: 844429225099264
	Start time: 26-11-2015: 21:03:17
	Trigger DigitalInput
		Channel: 1
		Until acknowledged: No
	Actions: Priority = Medium
		DigitalOutput: output = 1
		CameraRecording: camera = 1 time = 10 
                 resolution = <camera default=""> framerate = <camera default="">
		CameraPrerecording: channel = 1 time = 10
-----------------------------------------------------
Alarm ended. Guid: 844429225099264
	Start time: 26-11-2015 21:03:17
	End time  : 27-11-2015 17:51:03
	Trigger DigitalInput
		Channel: 1
		Until acknowledged: No
	Actions: Priority = Medium
		DigitalOutput: output = 1
		CameraRecording: camera = 1 time = 10 
                resolution = <camera default=""> framerate = <camera default="">
		CameraPrerecording: channel = 1 time = 10
-----------------------------------------------------

i want to output like this from Alarm ended data
Guid              starttime       	  Endtime
844429225099264  26-11-2015 21:03:17  	27-11-2015 17:51:03

but i am not getting this type of output
Alarm ended	 Guid	 844429225099264
Alarm ended	 Guid	 844429225099264
Alarm ended	 Guid	 844429225099264
 Start time	 26-11-2015 21	03
 Start time	 26-11-2015 21	03
 Start time	 26-11-2015 21	03
 Start time	 26-11-2015 21	03
 End time  	 27-11-2015 17	51
 End time  	 27-11-2015 17	51
Posted
Updated 28-Nov-15 1:25am
v2
Comments
PIEBALDconsult 28-Nov-15 8:46am    
Try a Regular Expression, seek only the Alarm ended sections. Looks pretty easy.
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
sadhana4 28-Nov-15 10:28am    
but how to get in one row guid startdate enddate

1 solution

Most of your problem is with
C#
string[] strlist = list[i].Split('.',':');
On the lines containing a date and time you are splitting the time section into pieces and not reconstructing it.

As PIEBALDconsult tried to guide you, the section for "Alarm ended" contains everything you need.

The file is also a fixed format, so once you have found the right section you can apply rules to how to get the lines containing the rest of the information.

1. The [Guid] can be obtained from the line containing text "Alarm ended"
2. The [Start Time] will be on the next line after "Alarm ended"
3. The [End Time] will be on the line after the line after "Alarm ended"
4. The data for each column will start after the first instance of ": "
5. The data for each column will end at the end of each line.

Which translates to
C#
string filename = @"c:\temp\DvrAlarmLog.txt";

if (!File.Exists(filename)) return;

var dt = new DataTable();
dt.Columns.AddRange(new[] { new DataColumn("ID"), new DataColumn("Start Date"), new DataColumn("End Date") });

var lines = File.ReadAllLines(filename);

for (var i = 0; i < lines.Length; i++)
{
    if (!lines[i].Contains("Alarm ended")) continue;

    //This section of the file contains everything we need
    var currGuid = lines[i].Substring(lines[i].IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 2);
    var startTime = lines[i + 1].Substring(lines[i + 1].IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 2);
    var endTime = lines[i+2].Substring(lines[i+2].IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 2);

    dt.Rows.Add(currGuid, startTime, endTime);
}
GridView1.DataSource = dt;
GridView1.DataBind();
 
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