Get name and create date of files in a directory

  • Thread starter Thread starter RJ
  • Start date Start date
R

RJ

Long time developer / total newbie @ C#.

I want to retrieve a list of files AND the file create date and load them
into an array for further processing…

I found the following which works well
Directory.GetFiles(sourceDir, "*.*"

But I can’t find anything similar to file and file create dates. Do I need
to retrieve the create dates one at a time (using foreach or for) based on
the results of GetFiles?
 
RJ said:
[...] Do I need
to retrieve the create dates one at a time (using foreach or for) based on
the results of GetFiles?

Well, you'd need a loop just to check the information, one way or the other.

In any case, no…you don't necessarily need to retrieve the creation
timestamp one at a time. You've probably already noticed the
appropriate methods in the System.IO.File class to get the information
you want. But, if you start with a DirectoryInfo object instead of
using the Directory class, the GetFiles() method returns an array of
FileInfo objects. You can simply get the other information from the
properties on the FileInfo.

Note of course that there's really not much practical difference between
the two approaches. FileInfo will still have to do the same basic file
system i/o operation to get the information as the equivalent File
method call would.

FileInfo is useful if you intend to retrieve a number of different
pieces of information about a file, or intend to inspect that
information multiple times. But otherwise, you might as well just call
the appropriate File method (GetCreationTime() or GetCreationTimeUtc()).

Finally, note that the single-argument overload to Directory.GetFiles()
is equivalent to passing "*" or "*.*" as the search pattern to the
two-argument overload. If you're not actually providing a specific
pattern to match, you might as well not pass a pattern.

Pete
 
Long time developer / total newbie @ C#.

I want to retrieve a list of files AND the file create date and load
them into an array for further processingƒ Ý

I found the following which works well
Directory.GetFiles(sourceDir, "*.*"

But I canƒ Tt find anything similar to file and file create dates. Do
I need to retrieve the create dates one at a time (using foreach or
for) based on the results of GetFiles?

DirectoryInfo directory = new DirectoryInfo(directoryPath);

foreach(FileInfo file in directory.GetFiles())
{
string name = file.Name;
string fullPath = file.FullName;
string directoryPath = file.DirectoryName;\
string extension = file.Extension;

//NOTE: each of these has a Utc variation
// useful for comparing on directories across the world
DateTime creationDateTime = file.CreationTime;
DateTime lastAccessDateTime = file.LastAccessTime;
DateTime lastWriteDateTime = file.LastWriteTime;
long lengthInBytes = file.Length;

//Load to your favorite collection object here
}

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Long time developer / total newbie @ C#.

I want to retrieve a list of files AND the file create date and load them
into an array for further processing?

I found the following which works well
Directory.GetFiles(sourceDir, "*.*"

But I can?t find anything similar to file and file create dates. Do I need
to retrieve the create dates one at a time (using foreach or for) based on
the results of GetFiles?

DirectoryInfo.GetFiles - it returns an array of FileInfo objects, containg the
information yoru after:

DirectoryInfo directory = new DirectoryInfo(sourceDir);
FileInfo[] files = directory.GetFiles();
 
Thank you all for your help! Gregory your post was quite helpful and after
playing around with your detailed suggestion I learned many things!

Thanks again everyone.
 
Thank you all for your help! Gregory your post was quite helpful and
after playing around with your detailed suggestion I learned many
things!

The MSDN library can be your best friend if you like epiphanies. :-)

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
I posted a thank you response but it never showed up. So here we are again.

Thanks to you all for the detailed responses. I learned many things on this
somewaht trivial problem.

Thanks again,
RJ
 
I posted a thank you response but it never showed up. So here we are
again.

Thanks to you all for the detailed responses. I learned many things
on this somewaht trivial problem.

I saw the response. Depending on your client, it may be an issue with
how the responses show up. If from a web forum, it takes a bit to fully
sync.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top