Odd "Access to the path 'xyz' is denied (System.IO.__Error.WinIOError)" error.

I have a rather odd problem…

I cache some data for my website in text files.

Every now and then when trying to read a file I get a “Access to the path ‘xyz’ is denied (System.IO.__Error.WinIOError)” error.

What I don’t understand is how it can have access most of the time but not every once in a while. I can even access the txt file directly via the web as its not protected in anyway.

Its not a concurrency problem, well I don’t think so, because I also get a “The process cannot access the file ‘xyz’ because it is being used by another process. (System.IO.__Error.WinIOError)”. I expect this one to some extent as the website is running multiple processes.

Does anyone know why I would be getting the “Access to the path ‘xyz’ is denied” error?

Cheers,

The process cannot access the file ‘xyz’ because it is being used by another process

Their is your answer. If the text file is being currently used, it will lock out any other activity trying to access the file, until it is free again.

Side note, why are you storing cached data in a text file? Not very secure or efficient in a multi-user website.

Multiple processes reading the file should not be a problem at all. Usually only a problem when writting to said file.

What you need to look into is using some Locking to lock it during writes, that should solve most of the problem.

I’ve solved similar matters before, we usually went with a dedicated loader and a db-based storage mechanisim to avoid file locking issues.

Yes, as stated above, it sounds like the file writer is not be disposed of correctly. When do you write to this file? As if you try reading it while you are writing to it, it will throw this error.

And on a side note, y not use xml to cache or a local database? Local db would be best option as it is highly optimised for multiple connections

Thanks for the ideas… I’ll double check the disposal.

There certainly could be multiple processes trying to access the file - but I assumed that would give the second error rather than the first.

As for why txt caching, easy, quick, simple, does the job - I can’t see any real benefit from a more complex (xml, db, etc) solution.

Its one of two, security reasons or file access denial. If it’s security reasons then it should happen all the time, so my best guess is file access issues.
How do you access the file? is the object disposed after each call to it?

Sorry I guess I wasn’t totally clear… thats not the error I’m asking about… I mentioned 2 errors the one you refer to I understand, and the other which I do not.

I should say cached content… its not variable, nor private, but its pulled from a database on a separate server and the connection sometimes goes down, hence the text files.