“The process cannot access the file, because it is being used by another process” on C#

C# error messages are usually very clear to understand but sometimes it’s hard to find a good way to solve the problem. Especially with I/O operations we need some tricks.

While closing a file with C# via StreamReader, StreamWriter, fopen, fclose, etc. and get this error: “The process cannot access the file, because it is being used by another process” even if closing, giving null value and disposing the identifier is not working, there’s a simple way to  do:

GC.Collect();

This command will run GarbageCollector and removes this error from the ErrorList.

Add a Comment

Your email address will not be published. Required fields are marked *