System.ArgumentException: 'Illegal characters in path.' Error - vb.net

Please help. I have a piece of code that's already working in other parks of my program, however fails to work when accessed by a certain form so i can't see there can be an error with it. Its an information storage project using text files. A screenshot of the exact code and the error:
I expected it to change the label text to the contents of the text file its trying to read.
Thanks everyone :)

Well, there must be one or more illegal characters coming from your "zoots1.txt" file!
Build the filename and see what it looks like:
Dim zoot1s As String
zoot1s = My.Computer.FileSystem.ReadAllText("zoot1s.txt")
Dim fileName As String
fileName = zoot1s + "c.txt"
MessageBox.Show(fileName)
Dim ClassStrain As String
ClassStrain = My.Computer.FileSystem.ReadAllText(fileName)
TempLabel3.Text = ClassStrain
Timer1.Start()
--- EDIT --
My bad. I Found the issue to be that there is a skip in text where it goes to a new line. As if i had added vbNewline to it. Is there any way to edit the text file and take away the last character so there isnt a new line.
Use the Trim() function to get rid of white space. Also use Path.Combine() to make sure the path is correctly separated from the filename with the correct number of backslashes:
zoot1s = My.Computer.FileSystem.ReadAllText("zoot1s.txt").Trim()
Dim fileName As String = System.IO.Path.Combine(zoot1s, "c.txt")

I think you just missed the most important thing, which is the whole specific path of your file that will be reading data from, specifically after the ReadAllText() method, so instead of this line:
zoom1s=My.computer.FileSystem.ReadAllText('zoot1s.txt")
You should edit it like this:
zoom1s=My.computer.FileSystem.ReadAllTex(My.Computer.FileSystem.CurrentDirectory & \zoot1s.txt")
I hope this can solve your problem.
^_^

Related

VB writeline writes corrupt lines to text file

Is it possible for the following code to produce NUL values within a text file?
var temp_str = "123456;1234567"
My.Computer.FileSystem.WriteAllText(Path & "stats.txt", temp_str, False)
It seems simple, but it writes quite often and I'm seeing several files that get accessed by the application that have Strings written to as:
When opening the file with Notepad++. Some other editors show just squares, and it seems like each character is represented by a block/NUL.
So far I've been unable to reproduce this on my test system. I just find the files on a COMX module's file system that's been running in the field and comes back faulty, but I've been seeing enough of these files to make it a problem that needs to be solved.
Does anyone have an idea to prevent this behaviour?
Hard to say what the problem is without more code, but try this if you want to replace the existing contents of the file:
Dim fileContent = "My UTF-8 file contents"
Using writer As IO.StreamWriter = IO.File.CreateText(fullPathIncludingExtension)
writer.Write(fileContent)
End Using
Or this if you want to append UTF-8 text:
Dim newLines = "My UTF-8 content to append"
Using writer As IO.StreamWriter = IO.File.AppendAllText(fullPathIncludingExtension)
writer.Write(fileContent)
End Using
If you want to append Unicode text, you must use a different constructor for StreamWriter:
Using writer As IO.StreamWriter = New IO.StreamWriter("full/path/to/file.txt", True, Text.Encoding.Unicode)
writer.Write(MyContentToAppend)
End Using
Note that the True argument to the constructor specifies that you want to append text.

VB.NET A generic error occurred in GDI+ when Bitmap.Save()

I'm now created an application that encode value into QR code, and turn into image. Then, I want to store the image to my local. But, I tried many solutions that I found on Google and here. The issue still prompt out unexpectedly everytime. My code and a sample error picture is shown below. Please help me!
A generic error occured in GDI+
Dim qrCodeObject As QRCodeEncoder = New QRCodeEncoder()
Dim image As Image
Dim bitmap As Bitmap
qrCodeObject.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE
qrCodeObject.QRCodeScale = 6
qrCodeObject.QRCodeVersion = 5
qrCodeObject.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H
image = qrCodeObject.Encode(value)
bitmap = New Bitmap(image)
bitmap.Save(value + ".jpg")
ANSWER: Always save file with different name and doesn't include any illegal characters
Path concatenation with simple strings is crap.
Use Path.Combine to build a path, because this ensures your path does
contain needed slashes if it would otherwise not be valid.
Dim Pat As String = Path.Combine(String.Concat(value, ".jpg"))
bitmap.save(Pat)
If this does not help, we need to know what "value" contains in your case.

Having issues with illegal characters in file path

I am writing a program in VB.NET which loops through a file with some file paths in it to perform an action on. The file paths in this file are each on a line, and i'm looping through the file like:
Dim FileContents As String
FileContents = System.IO.File.ReadAllText("C:\File.txt")
Dim FileSplit As String()
FileSplit = FileContents.Split(vbCrLf)
For Each ThisLine In FileSplit
Dim FileModified As Date
FileModified = System.IO.File.GetLastWriteTime(ThisLine)
'Do something here
Next
Contents of File.txt is:
Y:\Users\localadmin\Desktop\MakeShadowCopy\FileInfo.vb
Y:\Users\localadmin\Desktop\MakeShadowCopy\FindFiles.vb
Y:\Users\localadmin\Desktop\MakeShadowCopy\MakeShadowCopy.sln
Y:\Users\localadmin\Desktop\MakeShadowCopy\MakeShadowCopy.v12.suo
The loop works fine, but it is throwing an exception on the line with GetLastWriteTime() on it, saying that the path contains illegal characters, but it is just a normal string with a file path in it.
If anyone has any ideas, or know how to escape the string going into GetLastWriteTime() that would be much appreciated :)
Thanks!
Probably the lines in your file are not correctly vbCrLf terminated.
If this is the case the Split cannot divide correctly your input in lines and you end up with the whole text passed to the GetLastWriteTime.
Instead of using ReadAllText you could use ReadAllLines and let the work to split the lines to the Framework that knows how to handle the file line break and carriage return codes.
For Each ThisLine In System.IO.File.ReadAllLines("C:\file.txt")
Dim FileModified As Date
FileModified = System.IO.File.GetLastWriteTime(ThisLine.Trim())
Next
Also add a Trim to the ThisLine variable to remove some unseen character added erroneusly to the line
Two ideas:
Use For instead of For Each and ensure that you're getting exception on the very first iteration. If not, you may have issues with one specific file path. Check out iteration variable value if that is the case.
Open the file in a hex editor and ensure that each line is terminating properly. You might have either CR (10) or LF(13) character at the end but not both as normal in Windows.

VB.net will not read text file correctly

I've been trying to use StreamReader to read a log file. I cannot verify what it is encoded in, as when I open it in notepad++ and select ANSI encoding, I get this result:
I'm getting the characters needed when using ANSI but they are followed by things like [NULL][EOT][SOH][NUL][SI]
When I try and read the file in VB (using StreamReader or ReadAll) with ANSI encoding selected the resulting string I get back is completely wrong.
How could I read a file like this in VB.net?
You could use the IO.File.ReadAllText("File Location", encoding as System.Text.Encoding) method,
Dim textFromFile as string = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb", System.Text.Encoding.ASCII) 'Or Unicode, UFT32, UFT8, UFT7, BigEndianUnicode or default. Default is ANSI.
If you still don't get the text you need by using the default encoding (ANSI), then you can always try the other 6 different encoding methods.
Update...
It appears that your file is corrupt, using the code below I was able to get a binary representation of whatever is in the file, I got this,
1111111111111101000001110000010000000000000001010000000000010011000000000000100000000000000111100000000000100110000000000011100000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111110100000111000001000000000000000101000000000001001100000000000010000000000000011110000000000010100000000000111111111111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
The massive amount of null data would suggest that the file is corrupt, which would also explain why we are not getting a lot of data whenever we try to read the file.
The code,
Dim fileData As String = IO.File.ReadAllText("C:\Users\Jason\Desktop\login20130417.rdb")
Dim i As Integer = 0
Dim binaryData As String = ""
Dim ch As String = ""
Do Until i = fileData.Length
ch = fileData.Chars(i)
bin = bin & System.Convert.ToString(AscW(ch), 2).PadLeft(8, "0")
i = i + 1
Loop
As #Daniel A. White suggested in his comment, that file does not appear to be encoded like a "normal" text file. A StreamReader will not work in this situation. I would attempt to use a BinaryReader.
Rdb file? Never heard of it. Quick google makes it less clear - n64 database file, Darkbot, etc...
However considering the name you have, and the general look of the opened file, i would say its a binary file.
If you want to read the file in vb.net you'll need a library of sorts, and i can't help you with one until you are able to shed some light on what the file may be, or what it was created with.

read a specific line from a txt file in VB .net

In VB.net I'm trying to read in a specific line from a file. An example of the line in the txt file is:
[PATH] = "/this/directory/run.exe"
Now I understand how to open the file for reading and writing in VB, but I need to parse out the path in the "" (quotation marks). Any help would be greatly appreciated!!
--Adam
Finding the line depends on what its distinguishing features are, but basically the idea would be to use LINQ. For example:
Dim line As String = File.ReadAllLines(path).FirstOrDefault(Function (s As String) s.StartsWith("[PATH]")
This gets you the first line that begins with "[PATH]". If you need better discrimination you could use a more sophisticated match such as a regex.
You can then extract the path from the line as per Rubens' or SLaks' answers.
Dim path As String = thatLine.Split("""")(1)
Assuming that the path will never contain quotes, you can use a regular expression:
Dim regex As New Regex(".+=\s*""(.+)""")
Dim path As String = regex.Match(line).Groups(1).Value
Alternatively, you can search for the quotes and extract the part between them using string functions, like this: (This assumes that there will always be exactly two quotes)
Dim pathStart As String = line.IndexOf(""""c) + 1
Dim path As String = line.Substring(pathStart, line.LastIndexOf(""""c) - pathStart)