VB.NET - Appending text to the last line of text file leaves (sometimes) an empty line - vb.net

The intention of the software I am coding is to append a string to the last line of a text file. The problem I am experiencing is that depending on the situation, sometimes I get an empty line when I try to append text to the last line of a text file.
My text file is very simple. It contains a few lines of text and each line contains a few words. When I open the text file with Windows' Notepad (without editing the content of the text file) and I move the caret to the end of the entire file, sometimes the caret is positioned at the end of the last line with content (text), and sometimes it is positioned at an empty line that doesn't contain even a white space.
When the caret's ending position corresponds to the last char of a line that contains text, I don't experience any problem appending some text programmatically. The programmatically appended line is at a new line and there isn't any empty line between the old last line and the current last line in the text file.
The problem appears when I can move the caret in the text file to the last possible position and that position corresponds to an empty line. In that case, when I try to append a new line programmatically, I can see an empty line before the just added line.
I have tried different codes and techniques, but it always happens the same to me.
The code I am actually using is:
If stringLastLine.Length = 0 Then
Using sw As StreamWriter = New System.IO.StreamWriter(filePath, True)
sw.Write("Inserted text")
sw.Close()
End Using
ElseIf stringLastLine.Length > 0 Then
Using sw As StreamWriter = New System.IO.StreamWriter(filePath, True)
sw.Write(Environment.NewLine & "Inserted text")
sw.Close()
End Using
End If
Basically, I am trying to detect if the last line length is over zero, so it's a line with text content and I will use "environment.newline + string". If it's a line without content (length = 0) I add directly the string without using "environment.newline". In the case that length = 0 I always get an empty line in between.
Any idea to solve this problem?

How about trimming your text to ensure there's no end of line characters at the end (the whole text)? Right now you might have an issue with multiple blank lines:
text = text.TrimEnd({Convert.ToChar(10), Convert.ToChar(13)})
This handles the EOL characters.

Related

How to call one line at a time in LabVIEW read text file?

I would like to read just one line of text at a time using "Read from text file" function. After the passing of this line I would like to move on to the next line after the rest of the program iterates once. When I change the "Read from text file" function to "Read lines", I can no longer put an indicator on the front panel for text. How can I iterate one line at a time? How can I put an indicator on the front panel to display which line of text was read?
This what you want:
Open/Create/Replace File (with inputs open and read-only)
— inside While Loop:
Read From Text File (with Read Lines)
End While Loop on error from Read From Text File
--After While Loop:
Close File
You can process the data in the While loop, or index it and process outside.
Read the Text file
Use for loop, and add the shift register to it
Through the input shift register terminal, wire the text file output (Initial data)
In Front panel, Right Click >> String >> Additional String Function >> Search/Split String
Wire the Initial data (from shift register) to the input string terminal and Provide the necessary search string/ Character (In my case, I have used line feed constant)
Search/Split String has two output terminals; Substring before the match and match +rest of the String
Trime the whitespace and wire the match +rest of the String o/p to the o/p shift register; Take the desired o/p from Substring before the match terminal
Set the tunnel mode of the desired string as indexing so that each line in the file is indexed to an element in the array.
Note: N value of the for loop's iteration terminal is equal to the no of lines in the file.

How to get number of specific line in multi line text box after loop check in vb .net?

I have a multi-line text box that contains a number of blank lines and I want to check these blank lines by loop and then give me the number of that blank line.
For Each line As String In TextBox2.Lines
If line = "" Then
'line.linenumber
End If
Next

How to prevent line feeds (vbLf) and carriage returns (vbCr) in text boxes in a vb.net text file?

How to prevent line feeds (vbLf) and carriage returns (vbCr) in a text box that should only be one line, in a text file? It can happen that these carriage return characters and these new line characters are present (for example during a copy and paste) in the body of the text and thus make the reading of the file impossible. This has happened to me before, and I think I need to set up a check for the presence of these characters and thus warn the user to check his line before any recording. Thank you in advance. Claude.
Replace the CR and LF with nothing?
textBoxWithCrLf.Text.Replace(vbCr, "").Replace(vbLf, "")
Note, this doesn't modify the contents of the textbox, it generates a new string with no CRLF, so you'd use it like:
somefilewriter.SomeWriteStringMethod( textBoxWithCrLf.Text.Replace(vbCr, "").Replace(vbLf, "") )

Deleting a line from a multiline flxText

How can I delete the first line from a flxText?
I have tried text.textField.replaceText(0, text.textField.getLineLength(0), ""); but it only works once. I have also tried keeping count of the line that's being deleted and it doesn't work either.
edit: I would like to reopen this question to specify that I want to take into account both newlines and word wrap.
Considering the text contains the line breaks why not just split the text on \n, remove the lines you no longer need, and then set the text to the joined lines.
var lines = flText.text.split("\n");
lines.shift(); // removes the first line
flText.text = lines.join("\n");

How do I remove the box symbol that shows up for a line break when I output a string to a cell?

I have a userform where the user enters a description of their project into a textbox and it is stored in a string. When the user runs the "Generate Report" routine that string is output to a single cell. If the user entered line breaks, they are present in the output, but there is also a "box" character for each line break. I can manually delete the "box" character and the line break is still there in the cell, but how do I prevent the "box" character from showing up in the first place?
Your string is probably carrying a vbCrLf (Carriage Return + Line Feed). All you need to do is remove the CR of the CR+LF.
Try this:
(YourCell.Value Here) = Replace(YourString, vbCrLf, vbLf)
Good luck.