Results 1 to 14 of 14

Thread: Loading list of files into a pre-defined directory into a listbox, then loading them

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Location
    Reykjavik, Iceland
    Posts
    16

    Loading list of files into a pre-defined directory into a listbox, then loading them

    Hello,
    I have a slight issue with my code. I've got a code which copies all the names of files in a pre-defined location to a listbox. Then when you double-click on the file in the listbox the file loads into a richtextbox to the left of it.
    The program can also create the files. The program will create a text file then refresh the listbox, adding everything to it.

    When I go to open the file from the listbox by double clicking the debug crashes stating that the file is already in use. How do I get around this?

    Creating the new text file and loading it into the listbox
    Code:
     'Add new object
                Dim path As String = GlobalVariables.FileLocation & "\" & GlobalVariables.ProjectName & "\Objects\" & txtObjectName.Text & ".txt"
    
                ' Create or overwrite the file. 
                Dim fs As FileStream = File.Create(path)
    
                'Load Files
                Dim folderInfo As New IO.DirectoryInfo(GlobalVariables.FileLocation & "\" & GlobalVariables.ProjectName & "\Objects\")
                Dim arrFilesInFolder() As IO.FileInfo
                Dim fileInFolder As IO.FileInfo
                arrFilesInFolder = folderInfo.GetFiles("*.*")
                For Each fileInFolder In arrFilesInFolder
                    Interpreter.lstFileBrowser.Items.Add(fileInFolder.Name)
                Next
    Attempting to open the file from the listbox to a richtextbox
    Code:
    Dim myfile As String = Dir(GlobalVariables.FileLocation & "\" & GlobalVariables.ProjectName & "\Objects\" & lstFileBrowser.SelectedItem)
    
            Dim read As IO.StreamReader
            read = IO.File.OpenText(NewProject.FolderBrowser.SelectedPath & "\" & GlobalVariables.ProjectName & "\Objects\" & lstFileBrowser.SelectedItem)
            txtEditor.Text = read.ReadToEnd()
            read.Close()
    How can I resolve the issue?

    Thanks,

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    The file is still open, so whenever you try to open an already opened file it will throw the exception. One thing you should do is wrap your StreamReader in a Using statement:
    Code:
    Using read As IO.StreamReader = New IO.StreamReader(NewProject.FolderBrowser.SelectedPath & "\" & GlobalVariables.ProjectName & "\Objects\" & lstFileBrowser.SelectedItem)
        txtEditor.Text = read.ReadToEnd()
    End Using
    Edit - I'd also replace using the & to combine your path with IO.Path.Combine. It prevents any missing slashes and typos in general.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Location
    Reykjavik, Iceland
    Posts
    16

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    I have a slight issue with that, when I paste in the code the path (after StreamReader) is underlined and says "Overload resolution failed, no accessible new can be called without narrowing conversion"
    How is this solved?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    Hmm, it could be that it's treating it as an Object rather than a String. Turn option strict and option explicit on and see if it gives you an error.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Location
    Reykjavik, Iceland
    Posts
    16

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    No, that doesn't solve it.
    I get the following error:
    Code:
    Error	1	Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
        'Public Sub New(path As String)': Argument matching parameter 'path' narrows from 'Object' to 'String'.
        'Public Sub New(stream As System.IO.Stream)': Argument matching parameter 'stream' narrows from 'Object' to 'System.IO.Stream'.
    EDIT: Ok, so I tweaked it slightly and it works well!
    One issue is that when the listbox is getting populated it hasn't got a using statement - how can I implement that into my listbox population code?
    Last edited by Tashology; Oct 20th, 2014 at 03:22 PM.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    You've got something that is currently an object... what does your code look like now? We can only guess what the changes you've made are.

    -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??? *

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    No, that's exactly what I expected. This line here:
    Code:
    NewProject.FolderBrowser.SelectedPath & "\" & GlobalVariables.ProjectName & "\Objects\" & lstFileBrowser.SelectedItem
    Is being treated as an object rather than a string. Try doing something like this:
    Code:
    Using read As IO.StreamReader = New IO.StreamReader(IO.Path.Combine(NewProject.FolderBrowser.SelectedPath, GlobalVariables.ProjectName, "Objects", lstFileBrowser.Text))
        'Blah
    End Using
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Location
    Reykjavik, Iceland
    Posts
    16

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    Ok, my loading code is like so: (it is working as far as I know) EDITED
    Code:
    Using read As IO.StreamReader = New IO.StreamReader(IO.Path.Combine(NewProject.FolderBrowser.SelectedPath, GlobalVariables.ProjectName, "Objects", lstFileBrowser.Text))
                txtEditor.Text = read.ReadToEnd()
            End Using
    The above code is solved and works

    The part that isn't working is the code which loads the files from the pre-defined location to the listbox:
    Code:
    Dim path2 As String = GlobalVariables.FileLocation & "\" & GlobalVariables.ProjectName & "\Objects\"
                Dim folderInfo As New IO.DirectoryInfo(path2)
                Dim arrFilesInFolder() As IO.FileInfo
                Dim fileInFolder As IO.FileInfo
                arrFilesInFolder = folderInfo.GetFiles("*.*")
                For Each fileInFolder In arrFilesInFolder
                    Interpreter.lstFileBrowser.Items.Add(fileInFolder.Name)
                Next
    Could anyone correct the code that loads the files into a listbox (above) so it is closed - as this is called after the top code, when the top code is called an error occurs because the files are in use already.

    Thanks,
    Last edited by Tashology; Oct 20th, 2014 at 04:19 PM.

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    I'd change the path2 variable to implement the IO.Path.Combine:
    Code:
    Dim path2 As String = IO.Path.Combine(GlobalVariables.FileLocation, "Objects")
    Edit: You could also add all files to the ListBox a little bit more easily by using this:
    Code:
    Using fb As FolderBrowserDialog = New FolderBrowserDialog
    
        If fb.ShowDialog = DialogResult.OK Then
            lstFileBrowser.Items.AddRange(New IO.DirectoryInfo(fb.SelectedPath).GetFiles.Select(Function(p) p.Name).ToArray)
        End If
    End Using
    Last edited by dday9; Oct 20th, 2014 at 04:26 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Location
    Reykjavik, Iceland
    Posts
    16

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    Ok, I've added that adjustment but there's a slight issue.
    When I try to execute the code:

    Code:
    Using read As IO.StreamReader = New IO.StreamReader(IO.Path.Combine(NewProject.FolderBrowser.SelectedPath, GlobalVariables.ProjectName, "Objects", lstFileBrowser.Text))
                txtEditor.Text = read.ReadToEnd()
            End Using
    I get an error because the file is already being used - by the previous snippet:
    Code:
    Dim path2 As String = IO.Path.Combine(GlobalVariables.FileLocation & "\" & GlobalVariables.ProjectName, "Objects")
    
                Dim folderInfo As New IO.DirectoryInfo(path2)
                Dim arrFilesInFolder() As IO.FileInfo
                Dim fileInFolder As IO.FileInfo
                arrFilesInFolder = folderInfo.GetFiles("*.*")
                For Each fileInFolder In arrFilesInFolder
                    Interpreter.lstFileBrowser.Items.Add(fileInFolder.Name)
                Next
    How can I resolve this?

    EDIT: I added your edit as the load to the textbox but I need it to be automatic without the user selecting the location, which I added in, but it still throws the error of already being used.

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    I'm assuming that GetFiles is leaving the file open, but I'm not sure as to why. Perhaps somebody else could chime in.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  12. #12
    Addicted Member omundodogabriel's Avatar
    Join Date
    May 2013
    Posts
    177

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    Try using:
    Code:
    Dim FileName as string = IO.Path.Combine(NewProject.FolderBrowser.SelectedPath, GlobalVariables.ProjectName, "Objects", lstFileBrowser.Text)
    txtEditor.Text = System.Text.Encoding.ASCII.GetString(System.IO.File.ReadAllBytes(FileName))
    And see if it still throws the exception.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2014
    Location
    Reykjavik, Iceland
    Posts
    16

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    Ah,
    I've managed to get it, I'm not sure what happened but it works now.

    Thanks,
    Resolved.

  14. #14
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Loading list of files into a pre-defined directory into a listbox, then loading t

    Getting the list of file has nothing to do with opening the file... period. end of story. There is NO correlation between the two.
    At any rate, GetallBytes works because it opens the file reads it, AND CLOSES it properly. if it wasn't working before, then that means YOU WERE NOT CLOSING the file correctly.

    -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??? *

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