1

This is probably pretty basic, but I'm trying to find out how I can find the first file in a directory?

For example, if I've got these lines of code

Dim di as New IO.DirectoryInfo("C:\Test") Dim aryFi as IO.FileInfo() = di.GetFiles("*.txt") 

I can see that the aryFi contains a list of all files, but I need only one and I need the full path of that one file. How can I get it?

2
  • 1
    1st by what sort method? Alphabetically? Commented Nov 19, 2009 at 11:49
  • Hi, the files are all the same, so it doesn't matter which file really. Commented Nov 19, 2009 at 11:56

1 Answer 1

3

Something like:

Dim firstFullPath = di(1).FullPath 

(I'm assuming VB.NET is still 1-based. Otherwise di(0).FullPath.)

For production code you should check that the array isn't empty before trying this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx, this is great! :) Jon Skeet to the resque :) Btw, I just saw a funny post this other day from StackOverflow where is says that "even Jon Skeet can't parse HTML using regular expressions" :) Funny :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.