File i/o question

  • Thread starter Thread starter > Adrian
  • Start date Start date
A

> Adrian

Is there a simple way to find out
(1.) when a file is in use,
(2.) and when any file in a directory is in use,
so file access by another user can be blocked?

I am asking this because I have to change a
single user app into a multi-user app, while using
FileShare parameters isn't possible due to the
way the app was written.

Adrian.
 
Try and open it exclusively if it fails it is in use.

try
{
using (FileStream i = File.Open(filename, FileMode.Open,
FileAccess.Read, FileShare.None))
{
return true;
}
}
catch (IOException)
{
return false;
}
 
Is there a simple way to find out
(1.) when a file is in use,

Just try to open for writing and check if exception was thrown
(2.) and when any file in a directory is in use,
so file access by another user can be blocked?

Exception will be arised

--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Michael Nemtsev said:
Just try to open for writing and check if exception was thrown


Exception will be arised

--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

The problem is that in one directory there are data files and files that
contain all sorts of parameters, that are used by all sorts of shared
methods. So just using a FileShare parameter of the data files per
se won't do. The only thing I can think of conceptually, is ascertaining
whether the directory is in use or not, and blocking use of the data
files of it is. However, I am unable to conceive a method for doing that.

Adrian.
 
DeveloperX said:
Try and open it exclusively if it fails it is in use.

try
{
using (FileStream i = File.Open(filename, FileMode.Open,
FileAccess.Read, FileShare.None))
{
return true;
}
}
catch (IOException)
{
return false;
}

The problem is that in one directory there are data files and files that
contain all sorts of parameters, that are used by all sorts of shared
methods. So just using a FileShare parameter of the data files per
se won't do. The only thing I can think of conceptually, is ascertaining
whether the directory is in use or not, and blocking use of the data
files of it is. However, I am unable to conceive a method for doing that.

Adrian.
 
|
| | > > Is there a simple way to find out
| > > (1.) when a file is in use,
| >
| > Just try to open for writing and check if exception was thrown
| >
| > > (2.) and when any file in a directory is in use,
| > > so file access by another user can be blocked?
| >
| > Exception will be arised
| >
| > --
| > WBR,
| > Michael Nemtsev :: blog: http://spaces.live.com/laflour
| >
| > "At times one remains faithful to a cause only because its opponents do
| not
| > cease to be insipid." (c) Friedrich Nietzsche
|
| The problem is that in one directory there are data files and files that
| contain all sorts of parameters, that are used by all sorts of shared
| methods. So just using a FileShare parameter of the data files per
| se won't do. The only thing I can think of conceptually, is ascertaining
| whether the directory is in use or not, and blocking use of the data
| files of it is. However, I am unable to conceive a method for doing that.

There is no such thing like "Directory in use". If you want shared access to
files in an application, you have to open the files for shared read or
read/write access. For files shared for read-only access, there is nothing
special to be done, for files opened in R/W access mode you need to
coordinate the write accesses across the process boundaries, one way to do
this is by using a global mutex. If you wan't finer grained write access
control, you will have to lock file regions using FileStream.Lock/Unlock.

Willy.
 
Willy Denoyette said:
|
| | > > Is there a simple way to find out
| > > (1.) when a file is in use,
| >
| > Just try to open for writing and check if exception was thrown
| >
| > > (2.) and when any file in a directory is in use,
| > > so file access by another user can be blocked?
| >
| > Exception will be arised
| >
| > --
| > WBR,
| > Michael Nemtsev :: blog: http://spaces.live.com/laflour
| >
| > "At times one remains faithful to a cause only because its opponents do
| not
| > cease to be insipid." (c) Friedrich Nietzsche
|
| The problem is that in one directory there are data files and files that
| contain all sorts of parameters, that are used by all sorts of shared
| methods. So just using a FileShare parameter of the data files per
| se won't do. The only thing I can think of conceptually, is ascertaining
| whether the directory is in use or not, and blocking use of the data
| files of it is. However, I am unable to conceive a method for doing that.

There is no such thing like "Directory in use". If you want shared access to
files in an application, you have to open the files for shared read or
read/write access. For files shared for read-only access, there is nothing
special to be done, for files opened in R/W access mode you need to
coordinate the write accesses across the process boundaries, one way to do
this is by using a global mutex. If you wan't finer grained write access
control, you will have to lock file regions using FileStream.Lock/Unlock.

Willy.
***********************
Thank you,
Adrian.
 
Can't I simply put

After a filestream fs has been opened for read:
while(!fs.CanRead){}

After a filestream fs has been opened for write:
while(!fs.CanWrite){}

Adrian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top