Question Listing Files in an Array

DominicB

Active member
Joined
May 2, 2008
Messages
30
Programming Experience
Beginner
Hi all

OK, I didn't want to ask this 'cos I know the answer's going to be way too easy, but I've struggled with it for long enough really and I can't get to grips with it.

I found the code below on the internet. It lists all the files at a given location in ListBox1, and it works realy well, except I don't want the files in a listbox, I want them in an array, and I just can't manage it for some reason. I also tried MsgBox'ing dra from within the loop to see if it would report but it won't.

What am I doing wrong?

VB.NET:
' make a reference to a directory
        Dim di As New IO.DirectoryInfo("c:\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo

        'list the names of all files in the specified directory
        For Each dra In diar1
            ListBox1.Items.Add(dra)
        Next dra

DominicB
 
Well, actually diar1 is an array of FileInfo objects that each have a FullPath property you can access to get the info concerning the file it represents. So you already have an array of the files, just that they are represented by FileInfo object rather than just the String of their path...
 
Hi Stonkie
So you already have an array of the files, just that they are represented by FileInfo object rather than just the String of their path...
Thanks for the response. So how can I actually access this object? I assumed I could just MsgBox it like so :
VB.NET:
MsgBox (diar1(1))
but I get the error Argument 'Prompt' cannot be converted to type 'String'.

So how do I access what FileInfo contains?

DominicB
 
Try this :

VB.NET:
MessageBox.Show(diar(1).FullPath)

or maybe :

VB.NET:
MessageBox.Show(diar(1).OriginalPath)

There are also a few more properties you might be interested in from the FileInfo class. See here for a complete list : http://msdn.microsoft.com/en-us/library/system.io.fileinfo_members.aspx

EDIT : You could also simply try this :

VB.NET:
MessageBox.Show(diar(1).ToString())
 
Hi Stonkie

Thanks for that. OK, The line :
VB.NET:
MessageBox.Show(diar(1).ToString())
works a treat. The first two both won't run, and an error message is generated (attached). What could be causing this?

DominicB
 

Attachments

  • error.JPG
    error.JPG
    40.1 KB · Views: 48
Use the public properties of the FileInfo class. You see what's available on the object when you press the . (dot)
Intellisense can also tell you much info about these when you scroll the helper window, but I usually prefer to learn the class by browsing the class library documentation first.
 
My bad, those properties are hidden. I simply looked at the documentation I linked you to and didn't see the accessibility of these members...

I think you should look for some object oriented programming tutorial. Basically, diar(1) is an object and the dot allows you to look at what the object contains. It contains members like the path and stuff, but that data is generally hidden. Instead, you expose accessors so people can modify the data without knowing what actually happens (calling the function knowing only its meaning, not the implementation details). This allows you to use the FileInfo object without knowing when (if it does) and why it reads from disk to determine that the path is correct, for example.

I accidentally pointed you to the hidden properties instead of the exposed accessors.
 
Hi guys

I should have known it was a properties issue really.

Stonkie, thanks for the mini titorial on aceessors v properties. I do have a book about VB.Net 2008, bit it's really not good. I just looked up accessors in the index - not there. Neither is FileInfo, which is why I'm here. I need something else really.

Thanks for your help guys.

DominicB
 
Actually, I used general oriented terms. Accessors are called "properties" (but in truth, they could also be methods with the single purpose or returning a value) and properties would be "fields" or something like it in .NET. You may have better luck with those. When you declare an accessors property in VB.NET, you use the property keyword...

As for a good VB.NET book, I used a very good book to reassess my C# knowledge and I think they have a VB.NET equivalent, but I like to read reference material for cover to cover. Not for most people, and if I didn't have the base, I'd have been lost! I could give you the name later today (I don't have the book with me).
 
Hi all

Thanks go to all of you for your valued contributions - I think I have the code I need now to advance some more.

cjard, unfortunately, I could make use of your killer piece of code, as my software (VB.Net 2008 Express) doesn't like the Directory instruction : I found several examples on the 'net using Directory and they all fell down over that one instruction. I think it's an Express thing.

Thanks again everyone.

DominicB
 
Next silly question from me:
I remember typing the post
DominicB seems to have read it
But it is Nowhere To Be Found (on my screen)
 
Hi cjard

You did indeed type it.
I did indeed read it.
It's nowhere to be found on my screen either.

I didn't (can't) delete it, but I noticed when I clicked on Reply that the last post was then from Stonkie (no sign of yours) - I thought it was some clever stunt pulled on your behalf, and it never came back when I clicked Submit.

I admit that my descriptions of why the code didn't work weren't the best (it is late here:)) If you would be so kind as to post your "killer piece of code" again I will give you some info as to what actually happens.

DominicB
 
Try this:

Dim files as String() = Directory.GetFiles("C:\")

and tell me the error. It might be somehting as simple as not importing System.IO
 
Hi cjard
It might be somehting as simple as not importing System.IO
I've seen various bits of code that start with :
VB.NET:
Imports System.IO
and always got error reports with the code. So how do I import System.IO?

I've attached the error report that your code generates.

DominicB
 

Attachments

  • error.jpg
    error.jpg
    20.4 KB · Views: 35
Back
Top