Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine if a file is in use? - Resolved

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
Say, you need to read from a file. You have the code similar to this:
Code:
Dim loStreamReader As StreamReader
Try
	loStreamReader = New StreamReader(tcFullPath2File)

	lsFileContents = loStreamReader.ReadToEnd()
Catch loErr As Exception
	llGoOn = False
	lsErrMsg = "Failure reading text from file " & tcFullPath2File & "!" & vbCrLf & Read_Exception(loErr) & vbCrLf & _
					"Please exit this program and report this problem to the Developer."
	MsgBox(lsErrMsg, vbCritical, lsMsgHdr)
	lcRet = lsErrMsg
Finally
	If llGoOn Then loStreamReader.Close()
End Try
Sure thing, the line loStreamReader = New StreamReader(tcFullPath2File) will throw exception if the file is open by another process.
Sure thing, you'd want to prevent this from happening by checking if the file is not in use, right?
Besides the obvious solution, e.g. make a lil' function that does this check with Try-Catch construct and returns True if an exception's caught - here comes the question:

Is there in the VB and/or .NET some built-in/native function that checks if a file is in use?
(I honestly tried to find it myself in the VS 2019 Reference "book" - and couldn't find anything :-( )
Please advise.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top