Mar 28th, 2010, 07:20 PM
FileOpen automatic searching of hard drive to find file
I have a program that works I would just like it to be able to find the file with out me specifying the actual path. Because if it is on someone's computer other than mine the path might not be correct and it will not function properly. Here is the code I have done. The file it is looking for is of course names.txt. I have not been able to find much info on this.
The program as it stands, opens the file and reads each line of the file into an array and then sorts the array and outputs it to a text box.
Public Class SortFile
Dim strArray() As String
Private Sub SortFile_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer = 0
Dim result As String = ""
Try 'Check that file is in C:\
FileOpen(1, "c:\names.txt", OpenMode.Input)
Do Until EOF(1) 'Run until the End Of File.
If i = 0 Then
Else 'Increase array size by 1
ReDim Preserve strArray(UBound(strArray) + 1)
End If 'i = 0
Input(1, strArray(i)) 'Adding lines from the file to the array.
i = i + 1
'loop for output
For i = 0 To UBound(strArray)
result = result + strArray(i) & vbCrLf
TextBox1.Text = result
Catch 'If file not in C:\ display msgbox and end program.
MsgBox("Error opening file. " & vbCrLf & "Names.txt must be in C:\")
Mar 28th, 2010, 07:38 PM
Re: FileOpen automatic searching of hard drive to find file
First up, I strongly suggest getting rid of that old VB6-style file I/O. In VB.NET you should use members of the System.IO namespace. In your case, it's a simple matter of:
As for the question, how to get the file path depends on where it is. If it can be absolutely anywhere then you would most likely display an OpenFileDialog and have the user select the file visually. This is basically the same as opening a file in Notepad or any other document-centric Windows application.
strArray = IO.File
Click Here to Expand Forum to Full Width