Overwrite a file with FileSystemStorageProvider

Topics: Writing modules
Oct 7, 2011 at 6:58 PM

Hi all!

It seem that there is no possibility of overwriting a file properly with IStorageProvider (FileSystemStorageProvider). SaveStream() throws an exception if the file exists and DeleteFile throws one if it doesn't. This is especially problematic since there is no method for checking if the file exists: this means the only way to overwrite a file is to wrap the method into a try-catch, thus adding exceptions to normal program flow.

I think the preferred way of handling files that should be access from the outside world is through IStorageProvider, is this right? Am I overlooking something or there is really no way to do this without exceptions?

Thanks in advance!

Oct 7, 2011 at 7:27 PM

Apparently this is being taken care of in the 2.0 branch. In the meantime, you may have to catch those exceptions. Not ideal.

Oct 7, 2011 at 7:31 PM

Thanks, that's good news. It's nice to see issues taken care of, keep the good work up guys!

Dec 7, 2011 at 8:33 PM

I was just curious have been something done? I couldn't find any changes. IStorageProvider really should have an Exist() method.

Dec 7, 2011 at 8:51 PM

I've used IStorageProvider.TrySaveStream(...) which itself performs a try/catch.

Elsewhere I've just used File.Exists - this was for my ImageResizing integration so it's just for cached versions of resized images - which is still probably really bad but at least faster than catching an exception for work I have to perform sometimes multiple times in a normal request.

Dec 7, 2011 at 9:43 PM

File.Exists is what I've used formerly too but now I just try-catch. The reason is that AFAIK the implementation of IStorageProvider is meant to be interchangeable if the underlying storage system changes (like cloud storage), but File only works on local files.

Anyways, I've opened an issue here.