Assuming dates is a string[]
for (int i = 0; i < dates.Count; i++)
{
if(!string.IsNullOrEmpty(dates[i])
{
w.WriteLine(dates[i]);
}
}
w.Close();
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
StreamWriter w = new StreamWriter(folders[0] + "\\dates.txt", true);
for (int i = 0; i < dates.Count; i++)
{
w.WriteLine(dates[i]);
}
w.Close();
I think there is a space empty line at the bottom I can move down and then make backspace to delete the last empty line.
The problem is later I want to read this dates in the text file with another text file of dates and make a comparison between them.
The first date in one file against all dates in the other file and so on and if there is a duplicated dates to remove them from one of the lists.
Assuming dates is a string[]
for (int i = 0; i < dates.Count; i++)
{
if(!string.IsNullOrEmpty(dates[i])
{
w.WriteLine(dates[i]);
}
}
w.Close();
See if this works for you. The last foreach, rather then using Debug.WriteLine you could add the dates to a list for final results.
List<DateTime> results1 = File
.ReadAllLines("dates.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(line => DateTime.ParseExact(line, "dd/MM/yyyy HH:mm:ss",
CultureInfo.InvariantCulture)).ToList();
List<DateTime> results2 = File
.ReadAllLines("dates1.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(line => DateTime.ParseExact(line, "dd/MM/yyyy HH:mm:ss",
CultureInfo.InvariantCulture))
.ToList();
List<DateTime> duplicates = results1.Intersect(results2).ToList();
if (duplicates.Any())
{
foreach (var duplicate in duplicates)
{
Debug.WriteLine(duplicate);
}
}
Debug.WriteLine("");
List<DateTime> except = results1.Except(results2).ToList();
foreach (var dateTime in except)
{
Debug.WriteLine(dateTime);
}
Debug.WriteLine("");
foreach (var dateTime in results1)
{
if (results2.Contains(dateTime))
{
Debug.WriteLine($"{dateTime} is a dup");
}
else
{
Debug.WriteLine($"{dateTime} is not a dup");
}
}
Regarding the "blank line" at the end of the file, what
you are seeing is likely the result of WriteLine always
adding a newline at the end of the line it writes. That
is how each DateTime written appears on a separate line.
One way to avoid that is to use WriteLine for all lines
except the last one. Use Write instead of WriteLine for
the last item in the List.
int i;
for (i = 0; i < dates.Count - 1; i++)
{
w.WriteLine(dates[i]);
}
w.Write(dates[i]);
w.Close();