yan bellavance
yan bellavance

Reputation: 4850

Can I overwrite a file that is currently opened by another user

I have a couple of files which I have to update periodically through a process written in VB.net . These files are on a server and part of a domain that many users can access. These files should never be written to as they are for reference only. I need to be able to overwrite some of them even if a user has them opened. Is it possible to do this? Presently the files permissions are not set as read-only but I can do this as long as the user under which the process runs will still have permission to overwrite them.

UPDATE: thx for your responses. The files are pdf and are opened by clicking on them from windows file explorer. This also happens when a user simply has the file selected in windows explorer.

Upvotes: 3

Views: 8288

Answers (3)

wendell
wendell

Reputation: 1

Your code will generate an error if you try to modify a shared file and the said file is opened by the others on the network.

But if you put try catch block and the catch statement ignoring the exception of "Access to path...." message. Your update statement will successfully made.

ex:

try
{
    .
    .
    .
    your update statement here
    .
    .
}
catch (Exception ex)
{
    if (ex.Message.IndexOf("Access to path") < 0)
        throw ex;
}

Upvotes: 0

Eric
Eric

Reputation: 11662

Sometimes when another user has a file open for reading and you can't overwrite it, it is still possible to rename the file. So if you find that your write operation fails due to an existing locked file, you might try renaming the existing file to a temporary file name and then writing the new version. At some later time you can try deleting the old version with the temporary file name.

Upvotes: 2

Lucero
Lucero

Reputation: 60276

This depends on the sharing mode which was used when opening the files by the users. See the FileShare enum (which is accepted by some of the overloads which deal with files) for more information.

Upvotes: 3

Related Questions