.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

detecting a file in use

8 REPLIES 8
Reply
Message 1 of 9
Anonymous
400 Views, 8 Replies

detecting a file in use

I'm trying to delete an existing file and need to detect whether it is
currently open by another program. The following is a snippet of code I
tested it with:
Public Sub TestReadOnly

Dim CheckFile As New IO.FileInfo("c:\test.txt")

MsgBox("IsReadOnly = " & CheckFile.IsReadOnly)

End Sub

I open the test.txt file in Notepad and then run the above in AutoCAD. The
Msgbox says IsReadOnly = false. I would expect it to be true.

I'm stumped.
8 REPLIES 8
Message 2 of 9
arcticad
in reply to: Anonymous

The issue is notepad will not lock the file.
You will need to check the process list and see if the file is currently opened.

{code}
Public Class TextFile

Sub RunIt()
MsgBox(isTextFileInUse("c:\test1.txt"))
End Sub

Function isTextFileInUse(ByVal FullPath As String) As Boolean
Dim FileName As String = ""
If Not isFileInUse(FullPath) Then
FileName = System.IO.Path.GetFileName(FullPath)
If CheckNotepad(FileName) Then
Return True
End If
Else
Return True
End If
Return False
End Function

Public Function isFileInUse(ByVal sFile As String) As Boolean
If System.IO.File.Exists(sFile) Then
Try
Dim F As Short = FreeFile()
FileOpen(F, sFile, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
FileClose(F)
Catch
Return True
End Try
End If
End Function

Function CheckNotepad(ByVal textFile As String) As Boolean
Dim ProcessList As System.Diagnostics.Process()
Dim Proc As System.Diagnostics.Process

Dim rtnValue As Boolean = False
ProcessList = System.Diagnostics.Process.GetProcesses()
Try
For Each Proc In ProcessList
If Proc.ProcessName.ToUpper = "NOTEPAD" Then
If Proc.MainWindowTitle.Substring(0, textFile.Length).ToUpper = textFile.ToUpper Then
rtnValue = True
Exit For
End If
End If
Next
Catch ex As Exception
End Try
Return rtnValue
End Function

End Class
{code}
---------------------------



(defun botsbuildbots() (botsbuildbots))
Message 3 of 9
Anonymous
in reply to: Anonymous

You're confusing two different things.

The readonly attribute of a file means the file is read-only
and can't be written to.

You can open any file in notepad, read-only or not, but
you can't save changes to it in notepad if it's read-only.

Notepad doesn't put any kind of lock on a file when it
opens it, it just reads the file and displays its contents,
and doesn't access the file again until you try to save.

The standard test for detecting if a file is writeable is
to open it for write, using "append" mode, which will
not erase the existing file's contents if it succeeds,
and then immediately close the file if the open succeeds
(which means the file is writeable).

I don't know what that Notepad nonsense in the other
post is about, but you really should avoid cheesy hacks
like that, like the plauge.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327347@discussion.autodesk.com...
I'm trying to delete an existing file and need to detect whether it is
currently open by another program. The following is a snippet of code I
tested it with:
Public Sub TestReadOnly

Dim CheckFile As New IO.FileInfo("c:\test.txt")

MsgBox("IsReadOnly = " & CheckFile.IsReadOnly)

End Sub

I open the test.txt file in Notepad and then run the above in AutoCAD. The
Msgbox says IsReadOnly = false. I would expect it to be true.

I'm stumped.
Message 4 of 9
Anonymous
in reply to: Anonymous

This is getting interesting (and confusing). Tried the following code:

Try
Dim file As FileStream
file = New FileStream("c:\test.txt", FileMode.Append, FileAccess.Write)
FileClose()
MsgBox("file can be deleted")
Catch
MsgBox("file cannot be deleted")
End Try

It indicated that the file could be deleted whether the file was currently
open in Notepad or not.

Opened the file in Excel and then ran the above code - works as desired.

Opened file in Notepad and then tried to open it at the same time in Excel.
Got a message that the file is in use by another user.

So Excel can detect that the file is open by another user but my routine
does not. Do you know of another way to test the file?

"Tony Tanzillo" wrote in message
news:6327450@discussion.autodesk.com...
You're confusing two different things.

The readonly attribute of a file means the file is read-only
and can't be written to.

You can open any file in notepad, read-only or not, but
you can't save changes to it in notepad if it's read-only.

Notepad doesn't put any kind of lock on a file when it
opens it, it just reads the file and displays its contents,
and doesn't access the file again until you try to save.

The standard test for detecting if a file is writeable is
to open it for write, using "append" mode, which will
not erase the existing file's contents if it succeeds,
and then immediately close the file if the open succeeds
(which means the file is writeable).

I don't know what that Notepad nonsense in the other
post is about, but you really should avoid cheesy hacks
like that, like the plauge.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327347@discussion.autodesk.com...
I'm trying to delete an existing file and need to detect whether it is
currently open by another program. The following is a snippet of code I
tested it with:
Public Sub TestReadOnly

Dim CheckFile As New IO.FileInfo("c:\test.txt")

MsgBox("IsReadOnly = " & CheckFile.IsReadOnly)

End Sub

I open the test.txt file in Notepad and then run the above in AutoCAD. The
Msgbox says IsReadOnly = false. I would expect it to be true.

I'm stumped.
Message 5 of 9
Anonymous
in reply to: Anonymous

Seems like a lot of trouble to go through but it works and works well. If
nothing else, this environment is verbose. Thank you.

wrote in message news:6327410@discussion.autodesk.com...
The issue is notepad will not lock the file.
You will need to check the process list and see if the file is currently
opened.

{code}
Public Class TextFile

Sub RunIt()
MsgBox(isTextFileInUse("c:\test1.txt"))
End Sub

Function isTextFileInUse(ByVal FullPath As String) As Boolean
Dim FileName As String = ""
If Not isFileInUse(FullPath) Then
FileName = System.IO.Path.GetFileName(FullPath)
If CheckNotepad(FileName) Then
Return True
End If
Else
Return True
End If
Return False
End Function

Public Function isFileInUse(ByVal sFile As String) As Boolean
If System.IO.File.Exists(sFile) Then
Try
Dim F As Short = FreeFile()
FileOpen(F, sFile, OpenMode.Binary, OpenAccess.ReadWrite,
OpenShare.LockReadWrite)
FileClose(F)
Catch
Return True
End Try
End If
End Function

Function CheckNotepad(ByVal textFile As String) As Boolean
Dim ProcessList As System.Diagnostics.Process()
Dim Proc As System.Diagnostics.Process

Dim rtnValue As Boolean = False
ProcessList = System.Diagnostics.Process.GetProcesses()
Try
For Each Proc In ProcessList
If Proc.ProcessName.ToUpper = "NOTEPAD" Then
If Proc.MainWindowTitle.Substring(0,
textFile.Length).ToUpper = textFile.ToUpper Then
rtnValue = True
Exit For
End If
End If
Next
Catch ex As Exception
End Try
Return rtnValue
End Function

End Class
{code}
Message 6 of 9
Anonymous
in reply to: Anonymous

Well, opening file for writing may not be sufficient to detect if a file has
been already in use by other application (such as NotePad), even though it
works in most cases.

Some applications allow file it opens being shared by either open the file
in shared mode explicitly, or implicitly (NotePad).

So, in order to achieve your goal, you also need to pass the third parameter
FileShare.None enumeration value to various file/stream open method:

file = New FileStream("c:\test.txt", FileMode.Append, FileAccess.Write,
FileShare.None)

In this case, if the file/Stream cannot be opened exclusively/not shared,
the file is used by other app.

The approach in the other post (find NotePad process) is only useful if
NotePad is the only app that could open the file of your interested in. But
it is still unnecessarily complicated and not to the point at the issue in
hand.


"Ray S Whitmore" wrote in message
news:6327623@discussion.autodesk.com...
This is getting interesting (and confusing). Tried the following code:

Try
Dim file As FileStream
file = New FileStream("c:\test.txt", FileMode.Append, FileAccess.Write)
FileClose()
MsgBox("file can be deleted")
Catch
MsgBox("file cannot be deleted")
End Try

It indicated that the file could be deleted whether the file was currently
open in Notepad or not.

Opened the file in Excel and then ran the above code - works as desired.

Opened file in Notepad and then tried to open it at the same time in Excel.
Got a message that the file is in use by another user.

So Excel can detect that the file is open by another user but my routine
does not. Do you know of another way to test the file?

"Tony Tanzillo" wrote in message
news:6327450@discussion.autodesk.com...
You're confusing two different things.

The readonly attribute of a file means the file is read-only
and can't be written to.

You can open any file in notepad, read-only or not, but
you can't save changes to it in notepad if it's read-only.

Notepad doesn't put any kind of lock on a file when it
opens it, it just reads the file and displays its contents,
and doesn't access the file again until you try to save.

The standard test for detecting if a file is writeable is
to open it for write, using "append" mode, which will
not erase the existing file's contents if it succeeds,
and then immediately close the file if the open succeeds
(which means the file is writeable).

I don't know what that Notepad nonsense in the other
post is about, but you really should avoid cheesy hacks
like that, like the plauge.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327347@discussion.autodesk.com...
I'm trying to delete an existing file and need to detect whether it is
currently open by another program. The following is a snippet of code I
tested it with:
Public Sub TestReadOnly

Dim CheckFile As New IO.FileInfo("c:\test.txt")

MsgBox("IsReadOnly = " & CheckFile.IsReadOnly)

End Sub

I open the test.txt file in Notepad and then run the above in AutoCAD. The
Msgbox says IsReadOnly = false. I would expect it to be true.

I'm stumped.
Message 7 of 9
Anonymous
in reply to: Anonymous

{quote}

Opened file in Notepad and then tried to open it at the same time in Excel.
Got a message that the file is in use by another user.

{quote}

It doesn't work that way here. I open a file with notepad, then
opened the same file with Excel and the file opens in both.

Regardless of that, none of this makes any sense.

If you are going to delete the file, then you can try to delete it,
and if for any reason you can't, there will be an exception thrown
that you can catch, so what's the problem?

If you only want to know if a file can be deleted, you can use the
File.Move() method to rename the file, and catch the exception
that's thrown if the file can't be deleted, and if there is no exception
you can rename the file back to the original name, and then you
know if the file can be deleted without actually doing it.

Whether a file is 'in use' or not depends on whether the application
that opens the file specifies that the file can't be accessed at all, or
accessed for reading only.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327623@discussion.autodesk.com...
This is getting interesting (and confusing). Tried the following code:

Try
Dim file As FileStream
file = New FileStream("c:\test.txt", FileMode.Append, FileAccess.Write)
FileClose()
MsgBox("file can be deleted")
Catch
MsgBox("file cannot be deleted")
End Try

It indicated that the file could be deleted whether the file was currently
open in Notepad or not.

Opened the file in Excel and then ran the above code - works as desired.

Opened file in Notepad and then tried to open it at the same time in Excel.
Got a message that the file is in use by another user.

So Excel can detect that the file is open by another user but my routine
does not. Do you know of another way to test the file?

"Tony Tanzillo" wrote in message
news:6327450@discussion.autodesk.com...
You're confusing two different things.

The readonly attribute of a file means the file is read-only
and can't be written to.

You can open any file in notepad, read-only or not, but
you can't save changes to it in notepad if it's read-only.

Notepad doesn't put any kind of lock on a file when it
opens it, it just reads the file and displays its contents,
and doesn't access the file again until you try to save.

The standard test for detecting if a file is writeable is
to open it for write, using "append" mode, which will
not erase the existing file's contents if it succeeds,
and then immediately close the file if the open succeeds
(which means the file is writeable).

I don't know what that Notepad nonsense in the other
post is about, but you really should avoid cheesy hacks
like that, like the plauge.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327347@discussion.autodesk.com...
I'm trying to delete an existing file and need to detect whether it is
currently open by another program. The following is a snippet of code I
tested it with:
Public Sub TestReadOnly

Dim CheckFile As New IO.FileInfo("c:\test.txt")

MsgBox("IsReadOnly = " & CheckFile.IsReadOnly)

End Sub

I open the test.txt file in Notepad and then run the above in AutoCAD. The
Msgbox says IsReadOnly = false. I would expect it to be true.

I'm stumped.
Message 8 of 9
Anonymous
in reply to: Anonymous

This is a good thought but, again, since Notepad does not put a lock on a
file it opens, your procedure does not detect a file that has been opened by
Notepad.

"Norman Yuan" wrote in message
news:6327664@discussion.autodesk.com...
Well, opening file for writing may not be sufficient to detect if a file has
been already in use by other application (such as NotePad), even though it
works in most cases.

Some applications allow file it opens being shared by either open the file
in shared mode explicitly, or implicitly (NotePad).

So, in order to achieve your goal, you also need to pass the third parameter
FileShare.None enumeration value to various file/stream open method:

file = New FileStream("c:\test.txt", FileMode.Append, FileAccess.Write,
FileShare.None)

In this case, if the file/Stream cannot be opened exclusively/not shared,
the file is used by other app.

The approach in the other post (find NotePad process) is only useful if
NotePad is the only app that could open the file of your interested in. But
it is still unnecessarily complicated and not to the point at the issue in
hand.


"Ray S Whitmore" wrote in message
news:6327623@discussion.autodesk.com...
This is getting interesting (and confusing). Tried the following code:

Try
Dim file As FileStream
file = New FileStream("c:\test.txt", FileMode.Append, FileAccess.Write)
FileClose()
MsgBox("file can be deleted")
Catch
MsgBox("file cannot be deleted")
End Try

It indicated that the file could be deleted whether the file was currently
open in Notepad or not.

Opened the file in Excel and then ran the above code - works as desired.

Opened file in Notepad and then tried to open it at the same time in Excel.
Got a message that the file is in use by another user.

So Excel can detect that the file is open by another user but my routine
does not. Do you know of another way to test the file?

"Tony Tanzillo" wrote in message
news:6327450@discussion.autodesk.com...
You're confusing two different things.

The readonly attribute of a file means the file is read-only
and can't be written to.

You can open any file in notepad, read-only or not, but
you can't save changes to it in notepad if it's read-only.

Notepad doesn't put any kind of lock on a file when it
opens it, it just reads the file and displays its contents,
and doesn't access the file again until you try to save.

The standard test for detecting if a file is writeable is
to open it for write, using "append" mode, which will
not erase the existing file's contents if it succeeds,
and then immediately close the file if the open succeeds
(which means the file is writeable).

I don't know what that Notepad nonsense in the other
post is about, but you really should avoid cheesy hacks
like that, like the plauge.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327347@discussion.autodesk.com...
I'm trying to delete an existing file and need to detect whether it is
currently open by another program. The following is a snippet of code I
tested it with:
Public Sub TestReadOnly

Dim CheckFile As New IO.FileInfo("c:\test.txt")

MsgBox("IsReadOnly = " & CheckFile.IsReadOnly)

End Sub

I open the test.txt file in Notepad and then run the above in AutoCAD. The
Msgbox says IsReadOnly = false. I would expect it to be true.

I'm stumped.
Message 9 of 9
Anonymous
in reply to: Anonymous

I guess it all boils down to philosophy. I don't like a procedure that may
cause confusion for any user. If we simply delete the file in question, then
the Notepad user later gets an error message saying that the file exists
when he tries to save. So he is either confused and gets frustrated, maybe
checks the documentation (if I remembered to documentt this condition),
blames the "stupid" program, or calls his dealer for support (me).

Wish Microsoft would following its own guidelines on all its applications.

"Tony Tanzillo" wrote in message
news:6327719@discussion.autodesk.com...
{quote}

Opened file in Notepad and then tried to open it at the same time in Excel.
Got a message that the file is in use by another user.

{quote}

It doesn't work that way here. I open a file with notepad, then
opened the same file with Excel and the file opens in both.

Regardless of that, none of this makes any sense.

If you are going to delete the file, then you can try to delete it,
and if for any reason you can't, there will be an exception thrown
that you can catch, so what's the problem?

If you only want to know if a file can be deleted, you can use the
File.Move() method to rename the file, and catch the exception
that's thrown if the file can't be deleted, and if there is no exception
you can rename the file back to the original name, and then you
know if the file can be deleted without actually doing it.

Whether a file is 'in use' or not depends on whether the application
that opens the file specifies that the file can't be accessed at all, or
accessed for reading only.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327623@discussion.autodesk.com...
This is getting interesting (and confusing). Tried the following code:

Try
Dim file As FileStream
file = New FileStream("c:\test.txt", FileMode.Append, FileAccess.Write)
FileClose()
MsgBox("file can be deleted")
Catch
MsgBox("file cannot be deleted")
End Try

It indicated that the file could be deleted whether the file was currently
open in Notepad or not.

Opened the file in Excel and then ran the above code - works as desired.

Opened file in Notepad and then tried to open it at the same time in Excel.
Got a message that the file is in use by another user.

So Excel can detect that the file is open by another user but my routine
does not. Do you know of another way to test the file?

"Tony Tanzillo" wrote in message
news:6327450@discussion.autodesk.com...
You're confusing two different things.

The readonly attribute of a file means the file is read-only
and can't be written to.

You can open any file in notepad, read-only or not, but
you can't save changes to it in notepad if it's read-only.

Notepad doesn't put any kind of lock on a file when it
opens it, it just reads the file and displays its contents,
and doesn't access the file again until you try to save.

The standard test for detecting if a file is writeable is
to open it for write, using "append" mode, which will
not erase the existing file's contents if it succeeds,
and then immediately close the file if the open succeeds
(which means the file is writeable).

I don't know what that Notepad nonsense in the other
post is about, but you really should avoid cheesy hacks
like that, like the plauge.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

"Ray S Whitmore" wrote in message
news:6327347@discussion.autodesk.com...
I'm trying to delete an existing file and need to detect whether it is
currently open by another program. The following is a snippet of code I
tested it with:
Public Sub TestReadOnly

Dim CheckFile As New IO.FileInfo("c:\test.txt")

MsgBox("IsReadOnly = " & CheckFile.IsReadOnly)

End Sub

I open the test.txt file in Notepad and then run the above in AutoCAD. The
Msgbox says IsReadOnly = false. I would expect it to be true.

I'm stumped.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost