1

I was wondering if it was possible to check if the file "test.txt" is open? and if so then display a message that the file is in use? My biggest problem is that the file can be opened in notepad, word, excel etc. I have some basic coding, which checks if the file is open - what I am trying to do is check if the file is currently open and if its not in use then proceed with the coding, I have the following coding so far.

Dim Process() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
Dim Process2() As Process = System.Diagnostics.Process.GetProcessesByName("word")

For Each p As Process In Process
    If p.MainWindowTitle.Contains("test") Then
        MessageBox.Show("file open")
    Else
        'Run my code
    End If
Next

For Each p2 As Process In Process2
    If p2.MainWindowTitle.Contains("test") Then
        MessageBox.Show("file open")
    Else
        'Run my code
    End If
Next
LarsTech
  • 80,625
  • 14
  • 153
  • 225
JackSparrow
  • 389
  • 2
  • 8
  • 18
  • http://stackoverflow.com/questions/860656/how-does-one-figure-out-what-process-locked-a-file-using-c – JamieSee Jun 15 '12 at 23:11
  • 3
    Or http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use/937558#937558 – ChrisW Jun 15 '12 at 23:12
  • Thanks guys but both topics seem to contain a lot of coding when the coding I have posted more or less does the job in a few lines? – JackSparrow Jun 15 '12 at 23:20
  • For your code, you have to implement solutions for every possible app that can possibly edit a file. What if I open the file in TextEdit? Or WinPad? Will you code those also? What if I use Notepad to edit a file called c:\mycoolfile.txt, and you're wanting to track c:\anotherdir\mycoolfile.txt? Do all apps place the full filename in the MainWindow? I can't support your approach and suggest you keep researching. – ChrisW Jun 15 '12 at 23:58
  • Thanks chris, I will have a think about it. – JackSparrow Jun 16 '12 at 00:04
  • Hi Jack, as a general rule try to make the user experience pleasant, killing processes on them as suggested here isn't the WTG: http://stackoverflow.com/a/6716399/495455 – Jeremy Thompson Jun 16 '12 at 02:20

2 Answers2

1

Why dont you try performing an operation on the file (eg open as suggested by @ChrisW) and check if its locked instead:

eg

Catch ex As Exception
If TypeOf ex Is IOException AndAlso IsFileLocked(ex) Then
' do something? 
End If
End Try

Private Shared Function IsFileLocked(exception As Exception) As Boolean
    Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
    Return errorCode = 32 OrElse errorCode = 33
End Function
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

Many applications create temporary copies of a file when they load them. So it is difficult to check this. You could create a debugger to see which file is loaded by a binary which may be overkill.

Or use something like this:

try
{
  stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
}
catch (IOException)
{
  // the file is locked and not available to read or write.
  return true
}
finally
{
 // close the stream
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
mike
  • 172
  • 1
  • 3
  • Thanks Mike, to be honest the code I have in post 1 is working but I could just do with a little help on how I can join it together and if the file is not running then run my program. – JackSparrow Jun 15 '12 at 23:39
  • This is the same approach as my link in my comment, above. http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use/937558#937558 – ChrisW Jun 16 '12 at 00:01
  • @mike -1 you copied the answer and didn't even bother to convert it to VB.Net. – Jeremy Thompson Jun 16 '12 at 02:07
  • Reading each possible binary which has a file open is not trivial unless you walk through each of the current binaries in memory. There is no assurance that only word and notepad can open a txt file. @Jeremy Thompson: the post is note meant to feed code but explain the issue in this approach. – mike Jun 16 '12 at 19:45
  • ok, I think we didn't bother going that low level and assumed the OP would have at least saved the file once before trying to detect if it is open... – Jeremy Thompson Jun 17 '12 at 01:04