Question

I got the error "Invalid Characters in Path." My path is being output to a .txt file using the $findstr command and get deleted in the process of the application so I wrote a simple sub to output the string to a Textbox to see what was throwing the error.

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    Dim Folder = Application.StartupPath & "\PRODUCTION"
    Dim MyFile As String = File.ReadAllText(Folder & "\results3.txt")
    MyFile.Replace("/n", "") ' One of my various attempts
    TextBox2.Text = Folder & "\" & MyFile & "<--END"
End Sub

An example of what is showing up:

C:/user/.../file.asasm
<--END

For some reason the findstr command is adding a second blank line after my path and I have so far been unable to remove it. At the advice of several other threads on here I have tried:

MyFile.Trim() ' From MSDN is seems this only removes null spaces and not blank lines
MyFile.TrimEnd() ' Same result as trim
MyFile.Replace("/n","") ' I'm not sure if the /n parameter can be used in this way
MyFile.Replace(10,"") ' I thought this one for sure would work since 10 corresponds to a new line in the ASCII Table

Any help resolving this would be greatly appreciated. I realize there is several other threads on this topic, but none of the solutions seem to be working here.

REVISED: The solution I implemented in my code was

MyFile = MyFile.Replace(Envirnment.NewLine,"")
Was it helpful?

Solution

Have you tried:

MyFile.Replace(Environment.NewLine, "")

Which on Windows should be the same as:

MyFile.Replace(vbCrLf, "")

Also try:

MyFile.Replace(vbCr, "")

or

MyFile.Replace(vbLf, "")

Depending on the type of newline you have, either should work for you.

EDIT: regarding why MyFile.Replace(10,"") did not work for you. You became a victim of implicit type conversion (a feature of VB.NET), so this line was executed as:

MyFile.Replace("10","")

Please make sure you have Option Strict On, if you want to avoid such issues.

OTHER TIPS

Trim will also work MyFile = MyFile.Trim(vbNewLine.ToCharArray)

Environment.NewLine is probably the best way to refer to a newline. "/n" is definitely not. For one thing, you're surely thinking of "\n", not "/n" - and for another, in vb.net even backslashes are interpreted literally (not as a control character).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top