Results 1 to 12 of 12

Thread: File.ReadAllLines() with resources are rejected each time.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: File.ReadAllLines() with resources are rejected each time.

    What kind of file is embedded in My.Resources.vead?
    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 -

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    Re: File.ReadAllLines() with resources are rejected each time.

    @Stanav basic text files with lists of words.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    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.

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    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.

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: File.ReadAllLines() with resources are rejected each time.

    Try this:
    Code:
    Dim lines() as string = My.Resources.vead.Split(Environment.NewLine)
    If lines.Length > 15 Then
         RichTextBox1.Text = lines(15)
    End If
    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 -

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    17

    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.
    Attached Files Attached Files

  10. #10
    New Member
    Join Date
    Jan 2013
    Posts
    3

    Re: File.ReadAllLines() with resources are rejected each time.

    Pro Tip. Read up on Files Input Output
    http://www.programmer-tutorials.com/...-net-files-io/

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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....

    length = lines.Length ... nothing more, nothing less...

    only time you should use .ToString on a number like that... is if you're going to display it... and even then, there's (arguably) better ways.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width