phalanx
phalanx

Reputation: 497

How to close/unshare a file being used by another user?

My .NET app updates a certain .xls file located in a shared folder inside the domain. Using VB.NET, how can I check if that file is being used by another user and unshare/close it, so my code can update the file properly? NOTE:.Exe file(my app) will be executed in the server where shared folder is.

Upvotes: 0

Views: 890

Answers (1)

Monty Wild
Monty Wild

Reputation: 4001

Windows and NTFS is designed to prevent this scenario. Unless you:

  • Get the Excel application which is holding the .xls file open to close it
  • Shut down the server's network connection assuming the Excel file is opened by client PCs - a highly undesirable step to take
  • Discover the PC on which the Excel.exe instance is running and use the Windows Taskkill command-line command to kill all Excel.exe instances (since you won't necessarily know which one has it open) on the machine - another undesirable step to take
  • Have a look at this answer, but this uses third-party utilities, and doesn't explain how these utilities do what they do though you may be able to Process.Start them.

you just can't get Windows/NTFS to release the file lock.

A possible alternative that I'm not sure will work with an xls (as opposed to an xls_) is to make the file shared, though this has some limitations.

Upvotes: 2

Related Questions