RockGuitarist1
RockGuitarist1

Reputation: 49

Hard Time Writing NewLine in a .TXT File

Basically what I am doing is writing a program that pulls a quote from a website and writes it to a .txt file. It works fine except that I have no idea who to add NewLine into the .txt file. I will just show you the code.

If Not div Is Nothing Then
        Dim blank As String = Environment.NewLine
        Dim finish As String = (div.InnerText.Trim())
        TextBox2.Text = Chr(34) & finish & Chr(34)
        Dim fileName As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Horoscope", "Monthly.txt")
        My.Computer.FileSystem.WriteAllText(fileName, Chr(34) & finish & Chr(34), True)
        My.Computer.FileSystem.WriteAllText(Path.Combine(Environment.GetEnvironmentVariable("userprofile"), "Documents\Horoscope\Monthly.txt"), blank, True)
        My.Computer.FileSystem.WriteAllText(Path.Combine(Environment.GetEnvironmentVariable("userprofile"), "Documents\Horoscope\Monthly.txt"), blank, True)
    End If

Now that works fine for the first pair of quotes, but anything after it does not indent due to another section of code that I have that deletes duplicates.

Dim lines As String() = IO.File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Horoscope", "Monthly.txt"))
    lines = lines.Distinct().ToArray()
    IO.File.WriteAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Horoscope", "Monthly.txt"), lines)

Is there another way that I can get the same affect of having a gap inbetween my quotes in a text file?

Upvotes: 0

Views: 43

Answers (1)

Mark
Mark

Reputation: 8160

When removing the duplicates, you can drop the existing blank lines with Where, then add them back into the lines array using SelectMany:

lines = lines.
    Where(Function(x) Not String.IsNullOrEmpty(x)).
    Distinct().
    SelectMany(Function(x) { x, String.Empty }).
    ToArray()

The SelectMany returns the line, plus a blank, for each line left after the Distinct.

You may also want to use File.AppendAllLines when adding new entries - seems a little cleaner:

File.AppendAllLines(fileName, { Chr(34) & finish & Chr(34), ""})

EDIT

This would fit in with your code something like this:

If Not div Is Nothing Then
    Dim finish As String = (div.InnerText.Trim())
    TextBox2.Text = Chr(34) & finish & Chr(34)
    Dim fileName As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Horoscope", "Monthly.txt")
    IO.File.AppendAllLines(fileName, { Chr(34) & finish & Chr(34), ""})
End If
'...
Dim lines As String() = IO.File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Horoscope", "Monthly.txt"))
lines = lines.
    Where(Function(x) Not String.IsNullOrEmpty(x)).
    Distinct().
    SelectMany(Function(x) { x, String.Empty }).
    ToArray()
IO.File.WriteAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Horoscope", "Monthly.txt"), lines)

Upvotes: 1

Related Questions