|
-
Jan 17th, 2007, 04:59 PM
#1
Thread Starter
Member
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:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sFileToCheck As String = TextBox.Text
Dim sFileList() As String
If File.Exists(sFileToCheck) Then
sFileList = Directory.GetFiles(TextBox.Text)
ListBox1.Items.AddRange(sFileList)
Else
MessageBox.Show("The file does not exist")
End If
End Sub
End Class
-
Jan 17th, 2007, 07:04 PM
#2
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.
-
Jan 17th, 2007, 07:42 PM
#3
Re: help with filecheck program
Change this line
VB Code:
sFileList = Directory.GetFiles(TextBox.Text)
To this
VB Code:
sFileList = File.ReadAllLine(TextBox.Text)
-
Jan 17th, 2007, 07:51 PM
#4
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|