How do I populate a multi dimensional array with filenames and file versions in C# ?

bondy666
bondy666 used Ask the Experts™
on
I have a line of code in C# which neatly creates an array:

string[] sourceFiles = Directory.GetFiles(lblSourcePath.Text, "*.sys", SearchOption.AllDirectories);

What I would like it to do is get the version number of the file and populate the second part of the multi dimensional array, sourceFiles. Is this possible without a long winded solution? Obviously I can take the results of this and then get the version numbers afterwards and then populate an array but I would like to do it at the same time the array above is populated which would hopefully mean a speed increase.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015
Commented:
The file information can only be read as whole.
The only way to do what you want would be for you to loop through the directory and get the files one by one to build your 2 dimensions array. Something similar to the Dir command in Visual Basic. I do not think that there is an alternative in the framework.

Anyway, since it tould trigger a disk access on each loop, it would surely be slower than using GetFiles and looping though the information in memory.
Directory.GetFiles:
Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.
Only return the string is name of file. For more information you can use API function:
FindFirstFileEx, or FindNextFile
http://msdn.microsoft.com/en-us/library/aa364419(v=vs.85).aspx
Top Expert 2015

Commented:
minhvc is correct, Directory.GetFiles returns only the names of the files. I was mixing it with DirectoryInfo.GetFiles.

A DirectoryInfo object has a GetFiles methods, but it returns a collection of FileInfo objects that gives you all the information you need about the files instead of just the name.

Author

Commented:
Yes, maybe I should have included a little more of my code but I am using this at the moment and have a largish array of all the files paths in memory. I had just hoped there was a way to pick up the other file details at the same time it collected the filename details as I could then search it all in memory to enhance the speed of a search I want to perform.
I guess I'll have to stick with what I have.

Thanks
AndyAinscowFreelance programmer / Consultant

Commented:
I think the other experts are alluding to something like this one line of code.

FileInfo[] sourceFiles = DirectoryInfo.GetFiles("*.sys", SearchOption.AllDirectories);

Which works on the current directory

Author

Commented:
I have achived what I what I wanted using loops which is fine. It's a shame the method I wanted to use doesn't pick up more information but I guess that would make it slower. Thanks anyway.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial