Eric
Eric

Reputation: 708

Read-only file in use right after read-only set to false

I'm trying to use the following code to delete a read-only file.

var fileInfo = new FileInfo(saveLocation);
fileInfo.IsReadOnly = false;
fileInfo.Delete();

When it gets to the third line, the following exception is thrown

Message: The process cannot access the file '\\filepath\filename.pdf' because it is being used by another process.

Note: \\filepath\filename.pdf is not the actual file path, I'm just using it to replace a longer path

I've checked the file, and before the code runs, it is set to read-only, and after the code runs, it is not anymore.

Am I incorrect in thinking that when a file is opened as read-only it is not considered to by in use? I'm pretty sure that is true for Microsoft office files suck as .xlsx files, but maybe not for PDFs?

Ultimately, my goal is to be able to push an updated version of this file to a shared location even if some user has the file open on their machine, which is why I initially set it to be read-only.

Upvotes: 0

Views: 270

Answers (1)

Eric J.
Eric J.

Reputation: 150108

Message: The process cannot access the file '\filepath\filename.pdf' because it is being used by another process.

This is not the same as the file being read-only.

You can find out in code which process is locking the file

https://stackoverflow.com/a/20623311/141172

You can also find out from the command line

UPDATE

Based on your comments, it seems like you may want an exclusive lock on the file for the duration that you are processing it

open file in exclusive mode in C#

Command-line tool for finding out who is locking a file

Upvotes: 1

Related Questions