473,385 Members | 1,326 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,385 software developers and data experts.

Is there a way to check for "exclusive" access to a file?

Hi,

I have an application that watches a directory using FileSystemWatcher.
When a specific type of files comes over, I want to parse it and then move
it.

However, these files can be somewhat large, and the FTP process isn't
complete when I try to access the files. In those instances, an exception
is thrown when I try to access the file.

Is there a way that I can check to see if the files is ready for reading and
moving before I read it? That way I can wait until it's ready instead of
having the exception thrown.

Thanks.

-- Kent Iler
----------------------------------------------------------------------
ke**@iler.NOSPAM.com
Please CC: all replies via e-mail
Remove .NOSPAM from e-mail address
Jul 22 '05 #1
2 2903
The OS should not allow you to open it exclusively until it has been written
and closed. You can trap the error and keep trying, in a loop with a timer,
until you get exclusive access. Another way I used to use was to try renaming
it and trapping the error in a loop until it worked. The OS will not allow
you to rename it until you have exclusive access.
Jul 22 '05 #2
Well the best way without resorting to unmanaged API calls is to wrap
the function which opens the file in a try catch block and use
FileShare.None as the share parameter to a FileStream object.

Example:

try {
using (FileStream fs = new FileStream (@"C:\MyFile.txt",
FileMode.Open, FileAccess.Read, FileShare.None)) {
//Code here...
}
} catch (IOException ioEx) {
//Exception handling here.
}

Using FileShare.None flag will make sure that when opening a file, the
process has exclusive access to the file.

Jul 22 '05 #3

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

Similar topics

2
by: Dave Reid | last post by:
Hi everyone... I'm trying to open a text file in exclusive mode (meaning I want to lock out any other processes from accessing the file while I'm working with it). Here's my code: ifstream...
1
by: sixsoccer | last post by:
I posted this last week, and did not receive a response. I thought I would try one more time. Any help would be much appreciated. Thanks Bill I have three databases running, all with a...
18
by: Andre Laplume via AccessMonster.com | last post by:
I have inherited a bunch of dbs which are are shared among a small group in my dept. We typically use the dbs to write queries to extract data, usually dumping it into Excel. Most dbs originated...
2
by: Kent P. Iler | last post by:
Hi, I have an application that watches a directory using FileSystemWatcher. When a specific type of files comes over, I want to parse it and then move it. However, these files can be somewhat...
35
by: kaleolani65 | last post by:
I recently started a job at a medical staffing company, a database used to keep information was built by a lady there who has very limited Access experience (even less than me) They run...
17
by: teddysnips | last post by:
One of my clients has asked me to make a change to one of their Access applications. The application is a Front End/Back End standard app. I didn't develop it, but looking at it tells me that...
5
by: =?Utf-8?B?U3BlZWR5?= | last post by:
Sorry for aksing such a basic question but I have searched the internet high and low and none of the solutions seem to help. Here is my (not working) code: FileStream fs = new...
1
by: PW | last post by:
Hi, All of a sudden one of our clients can't use her application. She gets "You don't have exclusive access to your database at this time" when trying to open our application. The only thing...
2
by: ARC | last post by:
Hello all, When a user starts my app, I run re-attaching code if the links are not valid, etc. In this routine, I then check the version of the back-end database. If the version is off, I run...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.