Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
I have a txt file like this:
21/05/2020 17:05:00 ; info ; info ; info
I know how to get this part of the row "21/05/2020 17:05:00" and put it as a variable.
Now I have the problem that I don't know how to compare this date and hour that is in my txt file with the date and the hour of windows.. and if it don't mach I need to get a message.
Can someone tell how the code should be?

Thank you.

What I have tried:

This is what I did to get this part of the row "21/05/2020 17:05:00"

string[] liness = File.ReadAllLines(ofd.FileName);
if (liness.Length > 0)
{
string lastLine = liness[liness.Length - 1];
string[] columns = lastLine.Split(';');
if (columns.Length > 0)
{
string lastColumn = columns[columns.Length - 5];
Posted
Updated 21-May-20 6:33am

You first have to get a proper DateTime value out of the string from the file:
C#
// Top of file
using System.Globalization;

string dateString = /* get your date string here */;
DateTime dateValue = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);

Then you can compare it to the current datetime. However, this gets a litlle tricky since you may want to compare only on a part of the values (for example, do not take into account the milliseconds). The implementation depends on your specification.
 
Share this answer
 
v2
Comments
Member 14786879 21-May-20 12:18pm    
When I try your solution is give me this error: "It's not possible from convert 'string' to 'System.IFormatProvider'. Do you know why?
phil.o 21-May-20 12:25pm    
My bad, I corrected my answer.
Member 14786879 21-May-20 13:02pm    
I tried it again like:
string[] liness = File.ReadAllLines(ofd.FileName);
if (liness.Length > 0)
{
string lastLine = liness[liness.Length - 1];
string[] columns = lastLine.Split(';');
if (columns.Length > 0)
{
string date = columns[0];

string dateString = date;
DateTime dateValue = DateTime.ParseExact(dateString, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);

But it say that there is this error on last row: "String not recognized as a valid DateTime value."
phil.o 21-May-20 13:26pm    
Withot knowing what exact value there is in the string, it is hard to tell a solution. It is time for you to start a debug session and investigate this value which cannot be parsed. Do you know how to conduct a debug session?
Member 14786879 21-May-20 13:34pm    
For the exact value you mean what there is in the variable "date"? Because if you mean that in the txt file he is going to read this string "21/05/2020 17:05:00 ; others info ; other info ; other info" and I'm going to read just the first "column" that contains just this "21/05/2020 17:05:00".
No I don't know how to conduct a debug session, I'm looking on others forum on google that have my same problem, but at moment I didn't find an answer that's why I have registered here and asked for help
This is the same question you posted yesterday at https://www.codeproject.com/Questions/5268648/How-do-I-read-last-column-of-a-text-file-and-compa[^]. And you already have some suggestions.
 
Share this answer
 
Comments
Member 14786879 21-May-20 12:16pm    
Yes I know, but now I don't know how to resolve this part of the problem
Richard MacCutchan 21-May-20 12:19pm    
Why not? OriginalGriff already told you how to do it. If you have a problem with his suggestion then ask him for clarification, rather than posting the same question again.
C#
string lastColumn = columns[columns.Length - 5];

Why on earth are you doing that? Your date and time is the first column not the last one. You access it by
C#
string date = columns[0];
 
Share this answer
 
Comments
Member 14786879 21-May-20 13:04pm    
You are right, I'm sorry but I'm a new entry in this world as you can see. I'm trying to do this program as I can, but it seems it don't work.
I tried as you say and it work good, now I have to understand better how to go on
Richard MacCutchan 21-May-20 15:25pm    
Well we all had to start somewhere, and learn from scratch. But you would do better to get hold of some good teaching materials to get started. Trying to learn from a forum like this will take far longer. There is a very good free book that you can download from the great Charles Petzold. Go to .NET Book Zero by Charles Petzold[^] and have a look at it.

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