File.ReadAllLines() with resources are rejected each time.
So, Here is the question I have.
Why does, file located in stated area, work: File.ReadAllLines(AppPath & "\filename.txt")
While this one, files uploaded into resource and saved then restarted, File.ReadAllLines(My.Resources.filename) doesn't?
The files are in the correct locations. The resource is in the resource folder using the VB.net system and the appPath files are in the appPath be a copy and paste.
The error is the resources has illegal characters in the path name. What could be causing this problem?
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim appPath As String = My.Application.Info.DirectoryPath.ToString()
Dim reVeadAsString As String() = IO.File.ReadAllLines(My.Resources.vead)
RichTextBox1.Text = reVeadAsString(15)
End Sub
Re: File.ReadAllLines() with resources are rejected each time.
Files held in resources are stored as a byte array. A text file in resources cannot therefore be read as a text file (or indeed a file of any kind) because it isn't one. If you want to store text in a resource file you should do so as a string and split on CrLf to get the lines. If you insist on storing a text file in resources then you have no choice but to write the array to a temporary file and then read that in the normal way.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Re: File.ReadAllLines() with resources are rejected each time.
@Dunfiddlin That makes a lot of sense. So, it will be best not even to use the resource text file system and just reference to the folder inside the AppPath.
Re: File.ReadAllLines() with resources are rejected each time.
As Dunfiddlin explained, most files stored as byte array in my.resource except a few file types such as icon, image and text files. With text file, as in your case, it is stored the content of the file as a string, which is the equivalence of what you get when calling io.file.readalltext(filepath). Thus, to get to the individual lines of that string, you will need to split it into an array of text lines. If you don't want the user to mess with your file, or simply want to deploy a single exe file then embedding the file in my.resources is the way to go... Otherwise, it is easier/more efficient to work with a physical file than with a resource.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -
Re: File.ReadAllLines() with resources are rejected each time.
Thank you all for your help. I am trying to make a single instance program that calls from three different text files but single lines in each text file. I tried:
Code:
Dim lines As New ArrayList()
Try
Using reader As New StreamReader(My.Resources.vead)
While Not (reader.Peek() = -1)
lines.Add(reader.ReadLine())
End While
End Using
Catch ex As IOException
messagebox.Show("error","error")
End Try
RichTextBox1.Text = lines(15)
But Got the same error message. I have never pulled from the resources before and I'm trying to figure it out.
Re: File.ReadAllLines() with resources are rejected each time.
Thank you. So far, it's pulling from the file like I want it to, but the problem is it's pulling with new lines which breaks up how the program works.
Code:
Dim rand As New Random
Dim lines() As String = My.Resources.vead.Split(CChar(Environment.NewLine))
Dim length As Integer
Dim Rad As Integer
Dim vead As String
For i = 0 To 5
length = CInt(lines.Length.ToString())
Rad = rand.Next(1, (length - 1))
vead = lines(Rad)
RichTextBox1.Text = RichTextBox1.Text & " " & vead
Next i
I have attached the vead.txt file which I am pulling from.
Re: File.ReadAllLines() with resources are rejected each time.
You can't convert Environment.NewLine (or vbCrLf which makes the point for you!) into a char because it's two! You need to use the string delimiter syntax which is ..
Dim lines() As String = My.Resources.vead.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Re: File.ReadAllLines() with resources are rejected each time.
Also.... this line... is doing work that is unnecessary and dangerous...
length = CInt(lines.Length.ToString())
well, it's a bad habit at any rate...
Lines.Length returns an integer... it can't return anything else... so there's no point in using .ToString on it... ESPECIALLY if you're then going to convert it right back to an integer with CInt....