jocull
jocull

Reputation: 21115

Intercepting a filesystem write to a stream or byte array

I have a C library with a .NET wrapper (it's Shapelib in this case) that writes files (Shapefiles) to the filesystem using a path like C:\Path\To\Things\Filename.shp. However, writing the files to the filesystem isn't actually what I need. Once they're written, I have to read them into back into streams anyways to either deliver them via the web, add them to a zip file, or some other task. Writing them to the filesystem means I just have to track the clutter and inevitably clean them up somehow.

I'm not sure if there's anything like PHP's stream protocol registers where the path could be like stream://output.shp...

Is it possible to intercept the filesystem writing and handle this entire task in memory? Even if this can be done, is it horrible practice? Thanks!

Upvotes: 1

Views: 1031

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134015

The consensus is that this is "virtually impossible." If you really need to ensure that this is done in RAM, your best bet is to install a RAM disk driver. Do a Google search for [windows intercept file output]. Or check out Intercept outputs from a Program in Windows 7.

That said, it's quite possible that much, perhaps most, of the data that you write to disk will be buffered in memory, so turning right around and reading the data from disk won't be all that expensive. You still have the cleanup problem, but it's really not that tough to solve: just use a try/finally block:

try
{
    // do everything
}
finally
{
    // clean up
}

Upvotes: 1

Related Questions