FileOpen automatic searching of hard drive to find file-VBForums
Results 1 to 2 of 2

Thread: FileOpen automatic searching of hard drive to find file

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007

    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.

    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 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:\")
            End Try
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    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: 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

Survey posted by VBForums.