Hello again. Had yet another problem.

This is the following log:

Maalinger: Node: 1 Dato og KL: 2013-05-03 14:10:57 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:11:07 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:11:17 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:11:27 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:11:37 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:11:47 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:11:57 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:12:07 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$
Maalinger: Node: 1 Dato og KL: 2013-05-03 14:12:17 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$

Please disregard the norwegian. :P

So this is my current function, part of a wfa, btn click.

     private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime FromTime, ToTime;
                string Line;
                int lognr;
                bool FoundStart = false;
                bool Writing = false;
                bool FoundEnd = false;

                FromTime = dateTimePickerFra.Value;
                ToTime = dateTimePickerTil.Value;
                lognr = Convert.ToInt32(txt_SokNr.Text);


                StreamReader _in;
                StreamWriter _out;
                _out = File.CreateText("Searched in log");
                _in = File.OpenText("Log " + lognr + ".txt");

                 while(Writing ==false)  //
                 {
                     Line = _in.ReadLine();
                     if (Line != null)
                     {
                         FoundStart = Line.Contains(FromTime.ToString("yyyy-MM-dd HH:mm:ss"));
                     }
                     if (FoundStart)
                     {
                        _out.WriteLine(Line);
                         Writing = true;
                      }
                  }
                  while (Writing == true)  
                  {
                     Line = _in.ReadLine();
                     _out.WriteLine(Line);
                     if (Line != null)
                     {
                         FoundEnd = Line.Contains(ToTime.ToString("yyyy-MM-dd HH:mm:ss"));
                     }
                     if (FoundEnd)
                     {
                            Writing = false;
                     }
                   }

                _in.Close();
                _out.Close();

                System.Diagnostics.Process.Start("notepad.exe", "Searched in log"); 
            }
            catch 
            {
                MessageBox.Show("Remember to choose a log to search in");
            }

So, if i search between lets say 2013-05-03 14:10:37 and 2013-05-03 14:11:47 the code works great. But what if i write 2013-05-03 14:10:00 to 2013-05-03 14:12:00, it obviously wont work since "Contains" needs the spesific time.

So my question is, how can i find the first value greater then the given time. (Or equal too, of course).

Thanks in advance, Asotop

since "Contains" needs the spesific time.

What does that mean? What is wrong with "2013-05-03 14:10:00 to 2013-05-03 14:12:00"?

It wont find the time 2013-05-03 14:10:00, since it doesn't exist in the log. So my code will fail.

One way to do it might be to convert the strings to int then create a DateTime structureClick Here, then use the Compare function to find the line that contains the first time equal to or greater than the specified date and time.

Working on it, but how do I get the "yyyy-MM-dd HH:mm:ss" string to datetime again?

Never mind. Worked it out, to be continued.

Didn't solve it after all. Here a sample code i made, i get format exception when i try to parse the string to datetime.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = "Maalinger: Node: 1 Dato og KL: 2013-05-03 14:10:37 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$";
            string[] stringsplitt = new string[] { "Maalinger:", "Node:", "Dato og KL: ", "Puls:", "Blodtrykk:", "Kroppstemperatur:", "Respirasjonsrate:", "$" };
            string[] value;

            DateTime time = new DateTime();
            IFormatProvider culture = new CultureInfo("en-US", true);
            value = line.Split(stringsplitt, StringSplitOptions.None);

            Console.WriteLine(value[3]);

            time= DateTime.ParseExact(value[3], "yyyy-MM-dd HH:mm:ss",culture, DateTimeStyles.None);

            Console.WriteLine(time);

            Console.ReadKey();

        }
    }
}

Try something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace TimeStampcs
{
    class Program
    {
        static void Main(string[] args)
        {
        string line = "Maalinger: Node: 1 Dato og KL: 2013-05-03 14:10:37 Puls: 554 Blodtrykk: 376 Kroppstemperatur: 20 Respirasjonsrate: 20$";            
        string value = line.Substring(line.IndexOf("KL: ")+4,19);
        DateTime time = new DateTime();
        Console.WriteLine(value);
        time = DateTime.Parse(value);
        Console.WriteLine(time.ToString("yyyy-MM-dd HH:mm:ss"));
        Console.ReadKey();
        }
    }
}

The formatting is really only needed for display. all the parts of the DateTime are still the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.