Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I did a program where I can read last line of a text file selected with a button using open file dialog.
Now I have another problem.. The text file is something like that:
20/05/2020 10:46:00 --- ;another info; another info; another info
How is the code for:
- When I press the button that read the last line of the text file in the rich text box he also read the first column, that contains data and hour, and compare it with the windows data and if it not mach he give me an allarm?

Is that possible? How should the code come out?

Thank you.

What I have tried:

This is what I did at moment for read the last line of the text file:

private async void OpenFileBtn_ClickAsync(object sender, EventArgs e)
{
using(OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text File|*.txt", Multiselect = false })
{

if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader rd = new StreamReader(ofd.FileName))
{
//ReaderRichTxtBox.Text = await rd.ReadToEndAsync();
string[] lines = rd.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
ReaderRichTxtBox.Text = lines[lines.Length - 1];
}
}
}

}
Posted
Updated 19-May-20 23:19pm

The problem is twofold: text files don't have "lines", and they certainly don't have "columns".

While that may seem wrong - and the File.ReadAllLines method would imply it is - it's actually true: textfiles have no intrinsic format, and "lines" are only accessible because the text file contains "end of line" characters which can be interpreted as separating lines. But you can't "replace a line", "insert a line", "delete a line", or "move to the last line" with a text file easily, because they don't have real "lines".
Similarly, nothing in a text file enforces columns, although it is possible to interpret "special characters" as column delimiters, they still have the same problems as lines do!

So to read the "last line" you need to read all the others first, and discard them.
The simplest way to do that is:
C#
string[] lines = File.ReadAllLines(pathToFile);
if (lines.Length > 0)
   {
   string lastLine = lines[lines.Length - 1];
   ...
   }
And then identify a "column separator" that is unique to yoru file - your data implies that ';' might be being used, but you will need to check properly to be sure. When you have the column separator character, you can break the last line into seperate "columns":
C#
string[] lines = File.ReadAllLines(pathToFile);
if (lines.Length > 0)
   {
   string lastLine = lines[lines.Length - 1];
   string[] columns = lastLine.Split(';');
   if (columns.Length > 0)
      {
      string lastColumn = columns[columns.Length - 1];
      ...
      }
   }
 
Share this answer
 
Comments
Member 14786879 20-May-20 6:04am    
Ok i think i got it this thing, and then how can I compare the first column with the data and hour of windows?
OriginalGriff 20-May-20 6:19am    
Once you have the separated columns, use DateTime.TryParse or DateTime.TryParseExact to convert it to a DateTime value and then just compare it.
Member 14786879 20-May-20 7:30am    
Hmm now I'm trying what you said, but if I have something like this in the text file:
20/05/2020 13:28:00 ; other info ; other info
I use your solution as:
string[] lines = File.ReadAllLines(pathToFile);
if (lines.Length > 0)
{
string lastLine = lines[lines.Length - 1];
string[] columns = lastLine.Split(';');
if (columns.Length > 0)
{
string lastColumn = columns[columns.Length - 3];
...
}
}
But if I want to read another text file that have another "; another info" it does not work because I should correct the code as " string lastColumn = columns[columns.Length - 4];"
OriginalGriff 20-May-20 7:51am    
You're going to have to look at your text files, and come up with a plan as to "what data goes where" - I can't do that for you, I don't have any access to your files!
Richard MacCutchan 21-May-20 12:22pm    
Why are you doing that when the date and time is the first column? That is you access it by columns[0].
Use the various functions of the string class to find the space characters and split the string into its separate parts. You can then convert the date string to a date/time type and do your comparison.
 
Share this answer
 
v2
Comments
Member 14786879 20-May-20 5:37am    
Can you explain me better please? As a code example
Richard MacCutchan 20-May-20 5:52am    
OriginalGriff has given that in his solution above.

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