Sunday, 15 July 2012

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



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

process cannot access file 'filename' because beingness used process

what mean, , can it?

of course of study not real question; found many questions here on related error general guidance (like 1 nullreferenceexception , indexoutofrangeexception) may useful.

answer community wiki sense free edit , improve more mutual scenarios , suggestions.

what cause?

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 programme first of have check close file handle after each use. here illustration of code bug:

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

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

using (var stream = file.open("myfile.txt", filemode.open)) { // utilize 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 mutual pattern i/o operations: seek , in case of error wait , seek 1 time again (did inquire why, example, windows shell takes time inform file in utilize , cannot deleted?). in c# it's pretty easy implement (see improve examples disk i/o, networking , database access).

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

please note mutual error can see on stackoverflow:

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

in case readalltext() fail because file in utilize (file.open() in line before). open file before them not unnecessary wrong. same applies file functions doesn't homecoming 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 help (if file shouldn't open else need utility process explorer check who doing what).

ways avoid

when applicable utilize using statement open files. said in previous paragraph it'll actively help avoid many mutual errors (see this post illustration how don't utilize it).

if possible seek decide owns access specific file , centralize access through few well-known methods. if, example, have info file programme read , write should box i/o code within single class. it'll create debug easier (because can set breakpoint there , see doing what) , it'll synchronization point (if required) multiple access.

don't forget i/o operations can fail, mutual illustration this:

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

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

whenever it's possible apply retry pattern , if you're using filesystemwatcher consider postpone 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 origin , writing end have @ to the lowest degree 2 options.

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

2) utilize 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 illustration shown how open file writing , share reading, please note when read , write overlaps result undefined , info may invalid. it's situation must handled when reading. note doesn't create 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 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 within answer.

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

c# .net language-agnostic ioexception

No comments:

Post a Comment