потом проверить, если ли у тебя права на запись в нужную папку и на файлы, которые этим патчем будут меняться

В патче прописан неверный путь установки, нужно посмотреть куда установилась программа.
На ярлык на рабочем столе нужно нажать правой кнопкой— свойства и напротив объект посмотреть путь, и в патче выбрать этот путь!

I have some code and when it executes, it throws a IOException , saying that

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

What does this mean, and what can I do about it?

8 Answers 8

What is the cause?

The error message is pretty clear: you’re trying to access a file, and it’s not accessible because another process (or even the same process) is doing something with it (and it didn’t allow any sharing).


It may be pretty easy to solve (or pretty hard to understand), depending on your specific scenario. Let’s see some.

Your process is the only one to access that file
You’re sure the other process is your own process. If you know you open that file in another part of your program, then first of all you have to check that you properly close the file handle after each use. Here is an example of code with this bug:

Fortunately FileStream implements IDisposable , so it’s easy to wrap all your code inside a using statement:

This pattern will also ensure that the file won’t be left open in case of exceptions (it may be the reason the file is in use: something went wrong, and no one closed it; see this post for an example).

If everything seems fine (you’re sure you always close every file you open, even in case of exceptions) and you have multiple working threads, then you have two options: rework your code to serialize file access (not always doable and not always wanted) or apply a retry pattern. It’s a pretty common pattern for I/O operations: you try to do something and in case of error you wait and try again (did you ask yourself why, for example, Windows Shell takes some time to inform you that a file is in use and cannot be deleted?). In C# it’s pretty easy to implement (see also better examples about disk I/O, networking and database access).

Please note a common error we see very often on StackOverflow:

In this case ReadAllText() will fail because the file is in use ( File.Open() in the line before). To open the file beforehand is not only unnecessary but also wrong. The same applies to all File functions that don’t return a handle to the file you’re working with: File.ReadAllText() , File.WriteAllText() , File.ReadAllLines() , File.WriteAllLines() and others (like File.AppendAllXyz() functions) will all open and close the file by themselves.

Your process is not the only one to access that file
If your process is not the only one to access that file, then interaction can be harder. A retry pattern will help (if the file shouldn’t be open by anyone else but it is, then you need a utility like Process Explorer to check who is doing what).

Ways to avoid

When applicable, always use using statements to open files. As said in previous paragraph, it’ll actively help you to avoid many common errors (see this post for an example on how not to use it).

If possible, try to decide who owns access to a specific file and centralize access through a few well-known methods. If, for example, you have a data file where your program reads and writes, then you should box all I/O code inside a single class. It’ll make debug easier (because you can always put a breakpoint there and see who is doing what) and also it’ll be a synchronization point (if required) for multiple access.

Don’t forget I/O operations can always fail, a common example is this:

If someone deletes the file after File.Exists() but before File.Delete() , then it’ll throw an IOException in a place where you may wrongly feel safe.

Whenever it’s possible, apply a retry pattern, and if you’re using FileSystemWatcher , consider postponing action (because you’ll get notified, but an application may still be working exclusively with that file).

Advanced scenarios
It’s not always so easy, so you may need to share access with someone else. If, for example, you’re reading from the beginning and writing to the end, you have at least two options.

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

2) use FileShare enumeration to instruct OS to allow other processes (or other parts of your own process) to access same file concurrently.

In this example I showed how to open a file for writing and share for reading; please note that when reading and writing overlaps, it results in undefined or invalid data. It’s a situation that must be handled when reading. Also note that this doesn’t make access to the stream thread-safe, so this object can’t be shared with multiple threads unless access is synchronized somehow (see previous links). Other sharing options are available, and they open up more complex scenarios. Please refer to MSDN for more details.

In general N processes can read from same file all together but only one should write, in a controlled scenario you may even enable concurrent writings but this can’t be generalized in few text paragraphs inside this answer.

Is it possible to unlock a file used by another process? It’s not always safe and not so easy but yes, it’s possible.

My web method creates a pdf file in my %temp% folder and that works. I then want to add some custom fields (meta) to that file using the code below.

The class PdfStamper generates an IOException , whether I use its .Close() method or the using block just ends. The process that is still holding on to the file handle is the webdev web server itself (I’m debugging in VS2010 SP1).

No matter what I try, it keeps throwing the exception at st.Close(); , to be more precise:

The process cannot access the file ‘C:Users[my username]AppDataLocalTemp53b96eaf-74a6-49d7-a715-6c2e866a63c3.pdf’ because it is being used by another process.

Either I’m overlooking something obvious or there’s a problem with the PdfStamper class I’m as of yet unaware of. Versions of itextsharp used are and, the issue is the same.

Any insight would be greatly appreciated.

EDIT: I’m currently "coding around" the issue, but I haven’t found any solution.