File Handling

Signo.X

Well-known member
Joined
Aug 21, 2006
Messages
76
Location
Australia
Programming Experience
1-3
Hi all,

Im trying to do the following ,

Search a specific directory for specific files, and i use for that
diar1 As IO.FileInfo() = di.GetFiles()

then loop through the directory and get the desigered files, my problem is the following :
when i get the file that i need to read from the directory , i use fileInfo() , but when i read the file i need to use the streamReader(), how can i associated after locating the file using the fileInfo() to read it using streamReader() ?


Thanks in Advance,
-- signo.x
 
The FileInfo class has a FullName property that returns the file path, which you can pass to the StreamReader constructor.

Having said that, I would recommend using the Directory.GetFiles method instead. You do not need FileInfo objects if all you want are the paths of the files. If you use Directory.GetFiles then you don't create a DirectoryInfo object and you don't create FileInfo objects. You just create an array of file paths. That's all you need so that's a more afficient way to achieve it:
VB.NET:
For Each filePath As String In IO.Directory.GetFiles("folder path here")
    MessageBox.Show(IO.File.ReadAllText(filePath))
Next filePath
 
i've used the following before i read ur thread and it worked fine
Dim sr As New System.IO.StreamReader(strPath & io.fileInfo.file.Name)

Thanks again! :D

 
You can't possibly have used that code because the FileInfo class has no member named File and, in fact, no Shared members at all other than ReferenceEquals. If you've got something that works that's the main thing though.
 
Hi all,

Im trying to do the following ,

Search a specific directory for specific files, and i use for that
diar1 As IO.FileInfo() = di.GetFiles()

then loop through the directory and get the desigered files, my problem is the following :
when i get the file that i need to read from the directory , i use fileInfo() , but when i read the file i need to use the streamReader(), how can i associated after locating the file using the fileInfo() to read it using streamReader() ?


Thanks in Advance,
-- signo.x


here is some code that lists all the MP3 files in c:\temp AND BELOW IN A RECURSIVE SEARCH:

VB.NET:
        Dim di As New System.IO.DirectoryInfo("C:\temp")
        Dim mp3s As System.IO.FileInfo() = di.GetFiles("*.mp3", IO.SearchOption.AllDirectories)

        Dim sr As System.IO.StreamReader
        For Each mp3 As System.IO.FileInfo In mp3s
            sr = New System.IO.StreamReader([B]mp3.OpenRead()[/B])
        Next

its littered with System.IO because i didnt import the namespace

a FileInfo class has OpenRead, OpenWrite, OpenText etc = methods which return a filestream. Wrap this stream in a streamreader as i have done here and you can get your streamreader attached to the file

to disable recursive searching, remove the option or look at the other values in the SEARCHOPTION enum
 
You can't possibly have used that code because the FileInfo class has no member named File and, in fact, no Shared members at all other than ReferenceEquals. If you've got something that works that's the main thing though.

i think he meant:

strPath & <IO.FileInfo instance>.File.Name


wouldnt mind betting the code is less readable than the simplified version posted above.. :/
 
If you are only using the file names and no other properties of the FileInfo objects then creating DirectoryInfo and FileInfo instances is a waste. You should just use the Directory and File classes instead, which require no instances:
VB.NET:
Dim sr As IO.StreamReader
For Each mp3 As String In IO.Directory.GetFiles("C:\temp", "*.mp3", IO.SearchOption.AllDirectories)
    sr = New IO.StreamReader(IO.File.OpenRead(mp3))
Next mp3
Same result with fewer lines and fewer objects.
 
Back
Top