Search code examples
c#.netntfs

Passthru reading of all files in folder


I've got pretty unusual request: I would like to load all files from specific folder (so far easy). I need something with very small memory footprint.

Now it gets complicated (at least for me). I DON'T need to store or use the content of the files - I just need to force block-level caching mechanism to cache all the blocks that are used by that specific folder.

I know there are many different methods (BinaryReader, StreamReader etc.), but my case is quite special, since I don't care about the content...

Any idea what would be the best way how to achieve this?

Should I use small buffer? But since it would filled quickly, wouldn't flushing of the buffer actually slow down the operation?

Thanks, Martin


Solution

  • I would perhaps memory map the files and then loop around accessing an element of each file at regular (block-spaced) intervals.

    Assuming of course that you are able to use .Net 4.0.

    In psuedo code you'd do something like:

    using ( var mmf = MemoryMappedFile.CreateFromFile( path ) )
    {    
        for ( long offset = 0 ; offset < file.Size ; offset += block_size )
        {
            using ( var acc = accessor = mmf.CreateViewAccessor(offset, 1) )
            {
                acc.ReadByte(offset);
            }
        }
    }
    

    But at the end of the day, each method will have different performance characteristics so you might have to use a bit of trial and error to find out which is the most performant.