check if files are already in use?

sebasjuh

Member
Joined
Dec 6, 2007
Messages
13
Programming Experience
Beginner
Hello,

I got a little problem and i just can't find the right answer through google :confused:
So i thought i give it a try on this forum.

I have a project made in Visual Basic 2005 what copies a selected directory from one place to another place. But when i copie a folder where a file is in use i get an error.

So i wanted to make a function where it checks the selected directory for files that are already opened. But i can't find something on the net that works.

So my question can anybody help me with this? Or get me started with building a function that checks if files are already opened?
 
There is no method specifically intended to tell you whether a file is locked. Maybe WMI could be used to get a list of locked files from the OS but, if so, I don't know what it is. The way to tell would be to try to open each one and, if it fails, then the file is locked. Given the fact that you've just got to handle an exception anyway, why not make it the exception that you're talking about in the first place? Exceptions are not a crime. They're an integral part of programming. The idea is to avoid exceptions where you reasonably can and handle the rest. Just catch the exception and tell the user the operation failed because a file was locked.
 
And if you type in a dos prompt on a Windows Server, the command: net files
You see a list of alle the files in use. How does Windows determine those files?

And is there a way to get something like that in Visual Basic 2005?
 
I think you're mistaken. That file list contains the files that are locked by remote machines, not all the files that are in use on the system. If it was every file in use on the system the list would be enormous.
 
With that command i can see the files that are opened by all the remote computers. I can see all the .doc, .xls and .dwg files that are opened and that are stored on the server.

I got here a code sample, with this code he deletes a .dwg even when it is opened by a user. Because not every document get's locked when opened.
VB.NET:
        Dim str As New System.IO.FileInfo("c:\test.dwg")
        Try
            str.Delete()
        Catch ex As Exception
            MessageBox.Show("File is open")
        End Try

That's why i want to know if it's possible to get the same results from net files command in a vb.net function......
 
the Net Files command is for files on a remote computer (through a network) not the local computer
 
Where I work we also have a locked file problem. Is it possible to check each file in a directory on the network if the file is locked and who has locked it?
 
Sure, using the method in post #5 (except, i wouldn't try to delete it, i would try to open it for write access instead)

simply loop through the files list of the folder, trying each one

You have use a List(Of String) or something to show you which ones are locked and which ones didnt have a problem
 
Back
Top