D
DavidM
Hi --
I have a project I am working on that needs to read a file that another
application is creating. The application will need to wait until the file is
no longer in use.
I have done some Googling and there doesn't appear to be a good way to do
this in .NET. I'm reading that some folks saying the below approach is bad
as it causing an exception and exceptions are too expensive and shouldn't be
used for these types of scenarios.
If someone can provide some sort of guadance or best practice, I'd
appreciate it.
static bool FileInUse(string path)
{
try
{
//Just opening the file as open/create
using (FileStream fs = new FileStream(path,
FileMode.OpenOrCreate))
{
//If required we can check for read/write by using
fs.CanRead or fs.CanWrite
}
return false;
}
catch (IOException ex)
{
//check if message is for a File IO
__message = ex.Message.ToString();
if (__message.Contains("The process cannot access the file"))
return true;
else
throw;
}
}
I have a project I am working on that needs to read a file that another
application is creating. The application will need to wait until the file is
no longer in use.
I have done some Googling and there doesn't appear to be a good way to do
this in .NET. I'm reading that some folks saying the below approach is bad
as it causing an exception and exceptions are too expensive and shouldn't be
used for these types of scenarios.
If someone can provide some sort of guadance or best practice, I'd
appreciate it.
static bool FileInUse(string path)
{
try
{
//Just opening the file as open/create
using (FileStream fs = new FileStream(path,
FileMode.OpenOrCreate))
{
//If required we can check for read/write by using
fs.CanRead or fs.CanWrite
}
return false;
}
catch (IOException ex)
{
//check if message is for a File IO
__message = ex.Message.ToString();
if (__message.Contains("The process cannot access the file"))
return true;
else
throw;
}
}