473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File i/o question

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.
Nov 15 '06 #1
7 1240
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
(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.
Nov 15 '06 #2
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
Nov 15 '06 #3

"Michael Nemtsev" <ne*****@msn.comwrote in message
news:C0**********************************@microsof t.com...
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.

Adrian.

Nov 15 '06 #4

"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
(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.
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.
Nov 15 '06 #5

"Adrian <" <no*@all.accessiblewrote in message
news:45********************@dreader2.news.tiscali. nl...
|
| "Michael Nemtsev" <ne*****@msn.comwrote in message
| news:C0**********************************@microsof t.com...
| 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.
Nov 15 '06 #6

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>
"Adrian <" <no*@all.accessiblewrote in message
news:45********************@dreader2.news.tiscali. nl...
|
| "Michael Nemtsev" <ne*****@msn.comwrote in message
| news:C0**********************************@microsof t.com...
| 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.
Nov 15 '06 #7
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
Nov 15 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
21
by: siroregano | last post by:
Hi Everyone- I'm new to this group, and almost-as-new to asking programming questions publicly, so please forgive me if I miss a convention or two! I have a text file, around 40,000 lines...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
22
by: petermichaux | last post by:
Hi, I'm curious about server load and download time if I use one big javascript file or break it into several smaller ones. Which is better? (Please think of this as the first time the scripts...
2
by: sani8888 | last post by:
Hi everybody I am a beginner with C++ programming. And I need some help. How can I start with this program *********** The program is using a text file of information as the source of the...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
4
by: saytri | last post by:
Hi guys! I am making a quiz. The questions are stored in a binary file. I made an option in the quiz where a user can add a question to the binary file. Altough this works, the problem is that...
14
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.