How to check if file is already open or not??

Posted by Oswaldlily under ASP.NET on 3/2/2012 | Points: 10 | Views : 3259 | Status : [Member] | Replies : 4
How to check if file is already open or not??
if opened should display error message
"File already opened"




Responses

Posted by: Hmanjarawala on: 3/2/2012 [Member] Bronze | Points: 25

Up
0
Down
The short answer is NO.

You have to try/catch the file operation.

public bool FileIsLocked(string strFullFileName) {
bool blnReturn = false;
System.IO.FileStream fs;
try {
fs = System.IO.File.Open(strFullFileName,
IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.None);
fs.Close();
}
catch (System.IO.IOException ex) {
blnReturn = true;
}
return blnReturn;
}


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/2/2012 [Member] Starter | Points: 25

Up
0
Down
Its expecting me to have file in this path
C:\Program Files\Common Files\microsoft shared\DevServer\10.0

what is this?

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/2/2012 [Member] Bronze | Points: 25

Up
0
Down
This may be default path, where your system is looking for.
will you tell me what exact behavior you want?

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/2/2012 [Member] Silver | Points: 25

Up
0
Down
Hi,
These links may help you.
http://www.visualbasic.happycodings.com/Files_Directories_Drives/code19.html
http://www.codeproject.com/Questions/102975/how-to-check-if-a-file-exe-is-already-open

Thanks,
Sanjay

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response