Search code examples
c#filenamesfileinfogetfilessystem.io.fileinfo

C# Get File properties without using System.IO.FileInfo


is it possible to extract 'some' properties from a given file without using System.IO.FileInfo at all?

I mean, I love using FileInfo in cases where I just have a few files to deal with, but if, for example, I want to grab a list of file names from, let's say, half a million files and I decide to use FileInfo... it takes for-darn-ever!

I suspect that FileInfo loads a bunch of other stuff about that file into memory (is that correct?). If so, I feel like there should be a faster way to, say, just grab name or file extension.

On the other hand, I could just use Directory.GetFiles(myPath), but this gives me an array with the full paths for every file, while I just need the name per file! (I guess I could parse the path string to extract the name from it... would that be faster than using FileInfo?)

What other alternatives are out there if I want to accomplish this (grab the name of the file, or grab the file extension) faster?

Thank you very much!!


Solution

  • You're looking for the methods in the Path class.

    Specifically, Path.GetFileName() and Path.GetExtension().