469,343 Members | 5,395 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

Check if a file is allready open by other application

Hello NG !
I would like to test, if a file in my subtree is allready in use by an other
application.
Do anybody know how to get this information ? It is possible to get this
information ?

Thanks and regards.

Marcel Hug
Nov 16 '05 #1
4 28792
try
{
using(FileStream fs = File.OpenWrite(strFileName)
{
if(fs == null)
return;
}
}
catch(UnauthorizedAccessException e)
{
//Here you know that the file is open by another app
}

"Marcel Hug" <ma**********@ch.abbdotcom> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
Hello NG !
I would like to test, if a file in my subtree is allready in use by an
other
application.
Do anybody know how to get this information ? It is possible to get this
information ?

Thanks and regards.

Marcel Hug

Nov 16 '05 #2
On Thu, 20 Jan 2005 12:04:46 +0100, Marcel Hug wrote:
Hello NG !
I would like to test, if a file in my subtree is allready in use by an other
application.
Do anybody know how to get this information ? It is possible to get this
information ?

Thanks and regards.

Marcel Hug


Try to open the file exclusively. If that files, then something else is
using the file. Ex:

FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read,
FileShare.None);

Make sure to add proper exception handling around that and code to close
the file so it doesn't remain locked.
--
Tom Porterfield
Nov 16 '05 #3
Hi,

It's possible but doing so is complex , I know if only one program (
ProcessExplorer ) that does it, no idea how though :(

The best you can do is trying to open the file with no sharing and catch the
exception if this fail, not the best way but it works
use this code

bool opened;
try{

FileStream fs = new FileStream( file, FileMode.Open, FileAccess.Write,
FileShare.None);
fs.Close();
opened = false;
}
catch( FileNotFoundException ex)
{
//what to do now?
opened = false;
}
catch( UnauthorizedAccessException e)
{
opened = true;
}
If you are 100% sure the file exist remove the first catch block
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marcel Hug" <ma**********@ch.abbdotcom> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
Hello NG !
I would like to test, if a file in my subtree is allready in use by an
other
application.
Do anybody know how to get this information ? It is possible to get this
information ?

Thanks and regards.

Marcel Hug

Nov 16 '05 #4
Hi,

OpenWrite create the file if it's not exist, maybe this is not the
intention. but only to test if the file exist and is open.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Yitzhak Gootvilig" <Go*******@gmail.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
try
{
using(FileStream fs = File.OpenWrite(strFileName)
{
if(fs == null)
return;
}
}
catch(UnauthorizedAccessException e)
{
//Here you know that the file is open by another app
}

"Marcel Hug" <ma**********@ch.abbdotcom> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl...
Hello NG !
I would like to test, if a file in my subtree is allready in use by an
other
application.
Do anybody know how to get this information ? It is possible to get this
information ?

Thanks and regards.

Marcel Hug


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Raymond | last post: by
2 posts views Thread by Michael Sutherland | last post: by
6 posts views Thread by Dino Buljubasic | last post: by
4 posts views Thread by Alex Simic | last post: by
2 posts views Thread by Ronald S. Cook | last post: by
1 post views Thread by sunil | last post: by
6 posts views Thread by Ros | last post: by
5 posts views Thread by carlos | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.