Cannot access file because it's being used by another process
I am getting the following error when I try to run my program:
System.IO.IOException: 'The process cannot access the file because it is being used by another process.'
I googled around and most suggestions want you to close the file stream when you are done with a file however the files that I am triyng to rename don't have a close method.
Here is my code:
So when I run this on a directory, it works on the first and sometimes the second file until something gets in the way. So what I need to do is force my program to rename files.
Any solutions to this?5 Replies
You sure you're not reading the files you just renamed while iterating over the directory? And that you don't have any files open in another program?
I am iterating through two my of disks and then backing up all files:
The encrypt method here does have
Close();
implemented like at the end of it, I found the implementation from here so I just added close methods at the end of the method:
https://stackoverflow.com/questions/17627326/aes-256-file-encryption-c-sharp
I was thinking that maybe the Microsoft's RenameFile method has a close option or maybe it opens the file and then I can close it somehowYou're not awaiting it though
and it's async
Yes, it is, so what should I do here?
Yeah, kind of...
š
but no serriously, how do I do this?
I will come back with a solution then š¦
!ban 899204088112230421 developing ransomware