Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

File i/o question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
> Adrian < - 15 Nov 2006 11:55 GMT
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.
DeveloperX - 15 Nov 2006 13:18 GMT
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;
}

> Adrian < wrote:
> Is there a simple way to find out
[quoted text clipped - 8 lines]
>
> Adrian.
> Adrian < - 15 Nov 2006 14:06 GMT
> Try and open it exclusively if it fails it is in use.
>
[quoted text clipped - 23 lines]
> >
> > Adrian.

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.
Michael Nemtsev - 15 Nov 2006 13:26 GMT
> 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

Signature

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

> Adrian < - 15 Nov 2006 14:05 GMT
> > Is there a simple way to find out
> > (1.) when a file is in use,
[quoted text clipped - 5 lines]
>
> Exception will be arised

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.
Willy Denoyette [MVP] - 15 Nov 2006 15:00 GMT
| > > Is there a simple way to find out
| > > (1.) when a file is in use,
[quoted text clipped - 15 lines]
| 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.
> Adrian < - 15 Nov 2006 15:19 GMT
> | > > Is there a simple way to find out
> | > > (1.) when a file is in use,
[quoted text clipped - 25 lines]
>
> Willy.

***********************
Thank you,
Adrian.
> Adrian < - 15 Nov 2006 16:05 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.