locked
Directory Loading? RRS feed

  • Question

  • I wanted to load this file to the listbox on my form and here is the code I used:

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim reader As IO.StreamReader = New IO.StreamReader("C:\Desktop\WordList.txt")
    
            Do While reader.EndOfStream = True
    
                ListBox1.Items.Add(reader.ReadLine)
    
            Loop
    
            reader.Close()
    
            ListBox1.Items.Add("")
    
            ListBox1.SelectedIndex = 0
        End Sub
    When debugging, I received the following error message: DirectoryNotFoundException was unhandled. What is another way of loading the form? The reason to load the text document (WordList.text) on my desktop is that I want to convert the words in it into codes. I am building a dictionary and it is tidious to add each word manually.


    JM


    • Edited by CodeWizards Thursday, February 23, 2012 12:37 PM
    Thursday, February 23, 2012 12:27 PM

Answers

  • If you stored the file on your windows Desktop, then C:\Desktop is not the correct directory to be looking in. Try

    Dim filePath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Wordlist.txt")
    Dim reader As IO.StreamReader = New IO.StreamReader(filePath)

    Also for your loop, I think you mean

    Do Until reader.EndOfStream

    instead of

    Do While reader.EndOfStream = True 'Wrong

    • Marked as answer by CodeWizards Thursday, February 23, 2012 7:34 PM
    Thursday, February 23, 2012 12:58 PM

All replies

  • If you stored the file on your windows Desktop, then C:\Desktop is not the correct directory to be looking in. Try

    Dim filePath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Wordlist.txt")
    Dim reader As IO.StreamReader = New IO.StreamReader(filePath)

    Also for your loop, I think you mean

    Do Until reader.EndOfStream

    instead of

    Do While reader.EndOfStream = True 'Wrong

    • Marked as answer by CodeWizards Thursday, February 23, 2012 7:34 PM
    Thursday, February 23, 2012 12:58 PM
  • Hi CodeWizards,

    it's true, the Dyrectory does not exist in this special case.
    Check the Path of your desltoplink and you'll find out, the file is located in a directory similar:

    "C:\Users\YourUserName\Desktop\WordList.txt" because this is a special directory.

    Imports System.Text
    
    Imports System.IO
    Public Class Form1
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    Dim reader As IO.StreamReader = New IO.StreamReader("C:\Users\YourUserName\Desktop\WordList.txt")
    Do While reader.EndOfStream = True
    ListBox1.Items.Add(reader.ReadLine)
    Loop
    reader.Close()
    ListBox1.Items.Add("")
    ListBox1.SelectedIndex = 0
    End Sub
    
    End Class
    

    Success


    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 ans VB 2010 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Thursday, February 23, 2012 1:13 PM
  • Good, the code was not inserted correctly but I got the point. Thanks


    JM

    Thursday, February 23, 2012 7:35 PM