c# - IOException: The process cannot access the file 'file path' because it is being used by another process -


i have code , when executes, throws ioexception, saying

the process cannot access file 'filename' because being used process

what mean, , can it?

what cause?

the error message pretty clear: you're trying access file, , it's not accessible because process (or same process) doing (and didn't allow sharing).

debugging

it may pretty easy solve (or pretty hard understand), depending on specific scenario. let's see some.

your process 1 access file
you're sure other process own process. if know open file in part of program, first of have check close file handle after each use. here example of code bug:

var stream = new filestream(path, fileaccess.read); var reader = new streamreader(stream); // read data file, when i'm done don't need more file.delete(path); // ioexception: file in use 

fortunately filestream implements idisposable, it's easy wrap code inside using statement:

using (var stream = file.open("myfile.txt", filemode.open)) {     // use stream }  // here stream not accessible , has been closed (also if // exception thrown , stack unrolled 

this pattern ensure file won't left open in case of exceptions (it may reason file in use: went wrong, , no 1 closed it; see this post example).

if seems fine (you're sure close every file open, in case of exceptions) , have multiple working threads, have 2 options: rework code serialize file access (not doable , not wanted) or apply retry pattern. it's pretty common pattern i/o operations: try , in case of error wait , try again (did ask why, example, windows shell takes time inform file in use , cannot deleted?). in c# it's pretty easy implement (see better examples disk i/o, networking , database access).

private const int numberofretries = 3; private const int delayonretry = 1000;  (int i=1; <= numberofretries; ++i) {     try {         // stuff file         break; // when done can break loop     }     catch (ioexception e) {         // may check error code filter exceptions, not every error         // can recovered.         if (i == numberofretries) // last one, (re)throw exception , exit             throw;          thread.sleep(delayonretry);     } } 

please note common error see on stackoverflow:

var stream = file.open(path, fileopen.read); var content = file.readalltext(path); 

in case readalltext() fail because file in use (file.open() in line before). open file beforehand not unnecessary wrong. same applies file functions don't return handle file you're working with: file.readalltext(), file.writealltext(), file.readalllines(), file.writealllines() , others (like file.appendallxyz() functions) open , close file themselves.

your process not 1 access file
if process not 1 access file, interaction can harder. retry pattern (if file shouldn't open else is, need utility process explorer check who doing what).

ways avoid

when applicable, use using statements open files. said in previous paragraph, it'll actively avoid many common errors (see this post example on how not use it).

if possible, try decide owns access specific file , centralize access through few well-known methods. if, example, have data file program reads , writes, should box i/o code inside single class. it'll make debug easier (because can put breakpoint there , see doing what) , it'll synchronization point (if required) multiple access.

don't forget i/o operations can fail, common example this:

if (file.exists(path))     file.delete(path); 

if someone deletes file after file.exists() before file.delete(), it'll throw ioexception in place may wrongly feel safe.

whenever it's possible, apply retry pattern, , if you're using filesystemwatcher, consider postponing action (because you'll notified, application may still working exclusively file).

advanced scenarios
it's not easy, may need share access else. if, example, you're reading beginning , writing end, have @ least 2 options.

1) share same filestream proper synchronization functions (because it not thread-safe). see this , this posts example.

2) use fileshare enumeration instruct os allow other processes (or other parts of own process) access same file concurrently.

using (var stream = file.open(path, filemode.open, fileaccess.write, fileshare.read)) { } 

in example showed how open file writing , share reading; please note when reading , writing overlaps, results in undefined or invalid data. it's situation must handled when reading. note doesn't make access stream thread-safe, object can't shared multiple threads unless access synchronized somehow (see previous links). other sharing options available, , open more complex scenarios. please refer msdn more details.

in general n processes can read same file 1 should write, in controlled scenario may enable concurrent writings can't generalized in few text paragraphs inside answer.

is possible unlock file used process? it's not safe , not easy yes, it's possible.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -