1

EDIT I know the file is locked (i.e. I do not need to check if it is locked). Rather, I want to know how long it has been locked for. e.g. 1 hour, 24 hours etc.

Is it possible to determine how long a file has been locked for in C#?

I have an application that executes every 10 mins, and moves some files between directories. If a file is locked by a user then it obviously can't move it, so the application sends a warning email to the user.

I only want the email to be sent once - the application executes every 10 mins so at the moment if the user does not close the file they will receive an email every 10 mins.

Hence I thought I could use how long the file has been open for as an indicator as to whether or not to send an email.

But other solutions are welcome too if anyone has any ideas?

Cameron
  • 229
  • 1
  • 5
  • 17
  • I guess that you'll have to store that info for yourself in a file or database table and grab it from there when needed.. – Oscar Sep 13 '16 at 08:40
  • Possible duplicate of [How to check for file lock?](http://stackoverflow.com/questions/1304/how-to-check-for-file-lock) – Alex Sep 13 '16 at 08:43
  • Not a duplicate. I do not want to check to see if a file is locked. I already know it is locked. Rather I want to find out how long it has been locked for. For example, 1 hour, 24 hours etc. – Cameron Sep 13 '16 at 08:45

1 Answers1

2

Handles don't carry information about when they were created as far as I know. But you can simply store information on whether this file was locked and who was e-mailed about it during the last script run. If the file is still locked by the same user, then don't send another e-mail.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Thanks. The application closes after 10mins, removing any such information from memory. But I could save information to a database. – Cameron Sep 13 '16 at 08:41