Nathan Taylor
Nathan Taylor

Reputation: 24606

.NET - Reliably Maintaining File System from an Application

I have a mixed application that has data both in a database and in a physical file store maintained by my application. As I have been developing my application I have, on occasion, run into scenarios where I am moving or deleting a file from the hard drive through my application and for whatever reason something will go wrong and an exception is thrown. Currently I simply log this and move on.

Assuming the delete or move scenario, when I throw and log the exception I now have a rouge or possibly missing file taking up space and also possibly causing presentation errors within the application. Beyond manual maintenance of the file system, what are some reliable techniques for maintaining a file system from an application?

I am particularly interested in how to make sure, no matter what, a file I call Delete() on in my application is in fact deleted.

Upvotes: 0

Views: 220

Answers (2)

Michael A. McCloskey
Michael A. McCloskey

Reputation: 2411

Since you are already using a database in your application, you could consider creating a table to track the file system operations. For instance you could create a row containing the details of the file system operation you are about to perform, then perform the file system operation, and upon success either delete the row or mark it completed in the database. If your application fails and/or needs to be restarted, this would provide an easy mechanism to determine which file system operations did not complete successfully and need to be retried.

Upvotes: 2

JP Alioto
JP Alioto

Reputation: 45117

If you are using Vista or later, you can use the Transactional File System to ensure your operations are atomic. You can find some examples at Transactional File System Operations with some wrappers and the like.

Upvotes: 3

Related Questions