Results 1 to 2 of 2

Thread: FileOpen automatic searching of hard drive to find file

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    10

    FileOpen automatic searching of hard drive to find file

    Hi all,

    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.


    Code:
    Imports System.IO
    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
                        ReDim strArray(0)
    
                    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
    
                Array.Sort(strArray)
    
                'loop for output
                For i = 0 To UBound(strArray)
                    result = result + strArray(i) & vbCrLf
                Next
                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:\")
                End
    
            End Try
    
        End Sub

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

    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:
    vb.net Code:
    1. strArray = IO.File.ReadAllLines("c:\names.txt")
    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.

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