The process cannot accessibility the record "filename" since it is being offered by another process

What go this mean, and what have the right to I do around it?


*

*

What is the cause?

The error blog post is pretty clear: you"re make the efforts to access a file, and it"s not easily accessible because another procedure (or even the exact same process) is law something through it (and it didn"t enable any sharing).

You are watching: The process cannot access the file because it is being used by another process (write to disk)

Debugging

It may be pretty straightforward to deal with (or pretty tough to understand), depending upon your certain scenario. Let"s see some.

Your process is the just one to accessibility that fileYou"re sure the other process is your very own process. If you recognize you open up that file in another part of her program, then very first of all you have actually to check that you appropriately close the record handle after every use. Here is an instance of code through this bug:

var stream = brand-new FileStream(path, FileAccess.Read);var leader = brand-new StreamReader(stream);// review data indigenous this file, once I"m done i don"t need it any type of moreFile.Delete(path); // IOException: record is in useFortunately FileStream implements IDisposable, so it"s straightforward to wrap all your password inside a utilizing statement:

using (var currently = File.Open("myfile.txt", FileMode.Open)) // usage stream// below stream is not available and it has been closed (also if// an exemption is thrown and also stack unrolledThis pattern will also ensure the the document won"t it is in left open in instance of exceptions (it might be the factor the paper is in use: something went wrong, and no one close up door it; see this article for one example).

If whatever seems good (you"re certain you always close every record you open, also in case of exceptions) and also you have multiple working threads, then you have actually two options: rework your code to serialize file access (not constantly doable and not always wanted) or use a retry pattern. It"s a pretty usual pattern because that I/O operations: you shot to do something and also in instance of error you wait and try again (did friend ask yourself why, because that example, Windows shell takes part time to inform you the a document is in use and cannot it is in deleted?). In C# it"s pretty simple to carry out (see also far better examples around disk I/O, networking and also database access).

private const int NumberOfRetries = 3;private const int DelayOnRetry = 1000;for (int i=1; ns Please keep in mind a common error we see really often top top tastecraftedmcd.com:

var present = File.Open(path, FileOpen.Read);var content = File.ReadAllText(path);In this situation ReadAllText() will fail due to the fact that the record is in use (File.Open() in the heat before). To open up the paper beforehand is not just unnecessary but additionally wrong. The same uses to all document functions that don"t return a handle come the paper you"re functioning with: File.ReadAllText(), File.WriteAllText(), File.ReadAllLines(), File.WriteAllLines() and also others (like File.AppendAllXyz() functions) will all open and close the document by themselves.

Your procedure is not the only one to access that fileIf your procedure is not the only one to access that file, then interaction have the right to be harder. A retry pattern will aid (if the file shouldn"t be open up by anyone else yet it is, climate you require a energy like procedure Explorer to check who is doing what).

Ways come avoid

When applicable, always use using statements to open files. As claimed in vault paragraph, it"ll actively help girlfriend to protect against many usual errors (see this post for an instance on how not to usage it).

If possible, try to decide that owns accessibility to a specific document and centralize accessibility through a few well-known methods. If, for example, you have a data paper where your regime reads and writes, climate you have to box every I/O password inside a single class. It"ll make debug easier (because girlfriend can constantly put a breakpoint there and also see who is act what) and also it"ll be a synchronization suggest (if required) for multiple access.

Don"t forget I/O work can constantly fail, a usual example is this:

if (File.Exists(path)) File.Delete(path);If someone deletes the paper after File.Exists() but prior to File.Delete(), climate it"ll litter an IOException in a place where you may wrongly feeling safe.

Whenever it"s possible, use a retry pattern, and also if you"re using FileSystemWatcher, think about postponing activity (because you"ll get notified, yet an application might still be working solely with the file).

Advanced scenariosIt"s not constantly so easy, for this reason you might need to share access with who else. If, for example, you"re analysis from the beginning and writing come the end, you have actually at least two options.

1) share the same FileStream with proper synchronization functions (because it is not thread-safe). Check out this and this posts for an example.

2) use FileShare enumeration come instruct OS to permit other procedures (or other parts that your own process) to accessibility same paper concurrently.

using (var present = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))In this instance I showed exactly how to open up a record for writing and also share for reading; please keep in mind that as soon as reading and also writing overlaps, it results in unknown or invalid data. It"s a instance that need to be handled when reading. Likewise note the this doesn"t make access to the currently thread-safe, for this reason this object can"t be shared with multiple threads unless accessibility is synchronized in which method (see previous links). Various other sharing options are available, and they open up more complex scenarios. Please describe MSDN for more details.

In general N processes can read from same paper all together but only one have to write, in a managed scenario you may even allow concurrent writings however this can"t be generalised in few text paragraphs within this answer.

See more: The Vm Session Was Closed Before Any Attempt To Power It On.

Is it feasible to unlock a paper used by an additional process? It"s not constantly safe and not for this reason easy yet yes, it"s possible.