Results 1 to 4 of 4

Thread: help with filecheck program

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    59

    Unhappy help with filecheck program

    This is my question:
    Write a program with a text box, and button, and list box. The user is to place a pathname in the text box and click the button. The program should first check that the file exists, displaying a message box if it doesn’t. If it does, the program should read the file as a text, with one string per line, and place each string as a new item in the list box.

    Here is the code I have so far,when I run it i think it gives the error message correctly, but it gives me an error and then doesn't put the file names into the list. could someone help me out with this?? What am I doing wrong?:

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim sFileToCheck As String = TextBox.Text
    3.         Dim sFileList() As String
    4.  
    5.         If File.Exists(sFileToCheck) Then
    6.             sFileList = Directory.GetFiles(TextBox.Text)
    7.             ListBox1.Items.AddRange(sFileList)
    8.         Else
    9.             MessageBox.Show("The file does not exist")
    10.         End If
    11.  
    12.     End Sub
    13. End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help with filecheck program

    You are mixing up files and folders. You are NOT checking a file. You are checking a folder, i.e. a directory. File.Exists is going to return False every time if you pass it the name of a folder. You should be using Directory.Exists to check whether the specified directory exists and, if it does, get a list of the files it contains. That means that your sFileToCheck variable is incorrectly named and should be called sFolderToCheck.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: help with filecheck program

    Change this line
    VB Code:
    1. sFileList = Directory.GetFiles(TextBox.Text)
    To this
    VB Code:
    1. sFileList = File.ReadAllLine(TextBox.Text)

  4. #4
    Addicted Member
    Join Date
    Jan 2007
    Posts
    143

    Re: help with filecheck program

    If I were you, (depending on this program's purpose) I would probably add a open file dialog box, and let the filepath = OpenFileDialog1.FileName, so the user can select their file instead of typing it (I'm using the folder browser dialog box for a similar purpose in my current project)

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