how to open a file without knowing its name

jackjake

Member
Joined
Nov 26, 2003
Messages
7
There are many files in a dirctory and my program needs to open and read all those files one by one. But I wouldnt know the name of those files, so how should I code?
Thanks for any help!
 
it depends how you want to open the files , if you want to read text files you can use the System.IO.StreamReader , if you want to launch a file ( eg: an exe ) you can use Process.Start( filename here ). but to initially get the file names you can use IO.Directory.GetFiles, heres a quick example of reading just text files in a directory ...
Code:
        Dim strFiles As String() = IO.Directory.GetFiles("C:\")
        Dim sfile As String
        For Each sfile In strFiles
            If sfile.EndsWith(".txt".ToLower) Then
                Dim sReader As New IO.StreamReader(New IO.FileStream(sfile, IO.FileMode.Open))
                MessageBox.Show(sReader.ReadToEnd)
                sReader.Close()
            End If
        Next
 
Back
Top