473,752 Members | 9,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exclusive file access

Hello:
Im workin with an app that checks for incoming files to a given folder
using a FileSystemWatch er object. My problem is that if the file is
too big, the Created event fires before the file is completely copied
to the folder, and it result in IOException, the file is been used by
another process...
I thought that if i could get exclusive acces to the file it means
that the file is already copied in disc.
bool inUse = true;
FileStream strm = null;
do {
try {
strm = new FileStream(file Name, FileMode.Open,
FileAccess.Read , FileShare.None) ;
strm.Close();
strm.Dispose();
inUse = false;
}
catch (IOException) {
inUse = true;
LogHelper.Write Log("File " + fileName + " still in
process.");
if(strm != null) {
strm.Dispose();
}
}
} while (inUse == true);

But this code is a never-ending loop. Even if i see that the file is
already copied in the folder, i can't get inUse= true. Maybe cause
FileSystemWatch er have some reference to it...
Any ideas of how to make this code works? Or how to know when the
incoming file is completely copied to destination folder?
Thank you very much!

Oscar Acosta

Jun 27 '07 #1
3 2007

"cubaman" <os************ ******@googlema il.comwrote in message
news:11******** **************@ q69g2000hsb.goo glegroups.com.. .
Hello:
Im workin with an app that checks for incoming files to a given folder
using a FileSystemWatch er object. My problem is that if the file is
too big, the Created event fires before the file is completely copied
to the folder, and it result in IOException, the file is been used by
another process...
I thought that if i could get exclusive acces to the file it means
that the file is already copied in disc.
But this code is a never-ending loop. Even if i see that the file is
already copied in the folder, i can't get inUse= true. Maybe cause
FileSystemWatch er have some reference to it...
Any ideas of how to make this code works? Or how to know when the
incoming file is completely copied to destination folder?
Thank you very much!

Maybe if you know the file is of a certain size, you can check for size
limit and if it's limit, send your routine into a wait period using a
timer, maybe in the event.

I have seen one example in .Net Application Block code where the example was
using a FileWatcher and in the code it was making the FileWatcher wait.
Jun 28 '07 #2
On Jun 28, 2:21 pm, "Mr. Arnold" <MR. Arn...@Arnold.c omwrote:
"cubaman" <oscar.acostamo nte...@googlema il.comwrote in message

news:11******** **************@ q69g2000hsb.goo glegroups.com.. .
Hello:
Im workin with an app that checks for incoming files to a given folder
using a FileSystemWatch er object. My problem is that if the file is
too big, the Created event fires before the file is completely copied
to the folder, and it result in IOException, the file is been used by
another process...
I thought that if i could get exclusive acces to the file it means
that the file is already copied in disc.
But this code is a never-ending loop. Even if i see that the file is
already copied in the folder, i can't get inUse= true. Maybe cause
FileSystemWatch er have some reference to it...
Any ideas of how to make this code works? Or how to know when the
incoming file is completely copied to destination folder?
Thank you very much!

Maybe if you know the file is of a certain size, you can check for size
limit and if it's limit, send your routine into a wait period using a
timer, maybe in the event.

I have seen one example in .Net Application Block code where the example was
using a FileWatcher and in the code it was making the FileWatcher wait.
Hello Arnold, thanks for your reply.
No, file is coming over ftp, so I dont know size. Also, windows
reserve all necesary disc space before copying the file, so it reports
real size since the begin of copy proccess.
I also tried to read attributes, but file is no readonly while copy is
in process, as it shoul be...
Any other sugestion?
Thanks

Jun 30 '07 #3
On Jun 30, 2:50 pm, cubaman <oscar.acostamo nte...@googlema il.com>
wrote:
On Jun 28, 2:21 pm, "Mr. Arnold" <MR. Arn...@Arnold.c omwrote:
"cubaman" <oscar.acostamo nte...@googlema il.comwrote in message
news:11******** **************@ q69g2000hsb.goo glegroups.com.. .
Hello:
Im workin with an app that checks for incoming files to a given folder
using a FileSystemWatch er object. My problem is that if the file is
too big, the Created event fires before the file is completely copied
to the folder, and it result in IOException, the file is been used by
another process...
I thought that if i could get exclusive acces to the file it means
that the file is already copied in disc.
But this code is a never-ending loop. Even if i see that the file is
already copied in the folder, i can't get inUse= true. Maybe cause
FileSystemWatch er have some reference to it...
Any ideas of how to make this code works? Or how to know when the
incoming file is completely copied to destination folder?
Thank you very much!
Maybe if you know the file is of a certain size, you can check for size
limit and if it's limit, send your routine into a wait period using a
timer, maybe in the event.
I have seen one example in .Net Application Block code where the example was
using a FileWatcher and in the code it was making the FileWatcher wait.

Hello Arnold, thanks for your reply.
No, file is coming over ftp, so I dont know size. Also, windows
reserve all necesary disc space before copying the file, so it reports
real size since the begin of copy proccess.
I also tried to read attributes, but file is no readonly while copy is
in process, as it shoul be...
Any other sugestion?
Thanks
Well, I'll reply to myself, in case it may be usefull for someone
else. The only solution I got was using windows api. Here is the code:

using System;
using System.Runtime. InteropServices ;

namespace CampaignReceive rService {
public static class NativeMethods {
private const uint GENERIC_WRITE = 0x40000000;
private const uint OPEN_EXISTING = 3;

[DllImport("kern el32.dll", SetLastError = true)]
private static extern IntPtr CreateFile(stri ng lpFileName,
uint dwDesiredAccess ,
uint dwShareMode,
IntPtr
lpSecurityAttri butes,
uint
dwCreationDispo sition,
uint
dwFlagsAndAttri butes,
IntPtr hTemplateFile);

[DllImport("kern el32.dll", SetLastError = true)]
[return : MarshalAs(Unman agedType.Bool)]
private static extern bool CloseHandle(Int Ptr hObject);
public static bool IsFileInUse(str ing path) {
IntPtr fileHandle;
fileHandle = CreateFile(path , GENERIC_WRITE, 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (fileHandle.ToI nt32() == -1) {
return true;
}
else {
CloseHandle(fil eHandle);
return false;
}
}
}
}

Best regards.

Oscar Acosta
mcad

Jul 5 '07 #4

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

Similar topics

2
2967
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 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.
1
3259
by: Chuck Van Den Corput | last post by:
I have encountered a problem that I am hoping someone can shed some light on. I have a multi-user A97 app. All users have a personal front-end MDE accessing a shared back-end. There is no specific MDW file for the app as security rules are housed within the app itself. On a couple of occasions, different users have launched the app only
1
357
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 slightly different function: 1) Shared Database where data entry is done
1
3920
by: JohnC | last post by:
I have this exact same scenario. It is new and seems to be related to when we installed Adobe 7.0 Standard/Professional. We have an MDB on a LAN file server. Using Access 2K and Windows 2K. When the application is opened by someone, it seems that if one of the Adobe 7.0 (Standard or Professional) users susequently open the application, they get the error - "...You do not have exclusive permissions to the database. Changes that you...
18
7343
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 in MsAccess 97 or prior and have been converted to 2003. On occassion user 1 will open a db. When user 2 opens the db it will not let user 2 modify macros and what not. I can understand this and realize we could split the db; it is not worth ...
1
3682
by: CJJ | last post by:
hi Folks, I am opening my access database file (Access 2003) from a C# application. The connection string specifies mode=Share Exclusive The intent is that the application have exclusive access to the database file. No other instance of MS Access or this particular C# application (or any other app) should be able to open the access database file concurrently.
35
24453
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 Access2k3, on WinXP desktops The files and DB reside on the corporate server. She complains that in the mornings the first person who opens the DB locks all others out, once they logout and the others log in everyone can begin working. Through research...
17
3922
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 it was done entirely using the Wizards. There is no log-in procedure. They want to amend it so that only one person can be logged in at any one time. So, if Joe Bloggs tries to open the application, and Fred Jones has already connected, Joe...
0
4970
by: Salad | last post by:
A97. Split database. Frontend DB1.MDB. Backend DB1BE.MDB. Tables are linked between DB1 and DB2. From my testing, if I open DB1 exclusively using /excl the backend DB1BE is not opened exclusively. If I open DB1 but have no tables open and then open up DB1BE exclusively, DB1BE will open exclusive. But if I then attempt to open a linked table in the frontend DB1 I'm informed I can't use the table because the file is in use.
2
6231
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 code that then upgrades their back-end db by adding new fields, etc. However, if new fields are needed, I do run a function that I wrote that is supposed to determine if there are other user's in the back-end database. If so, it tells them to...
0
9020
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9616
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9371
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8282
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4910
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3340
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2237
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.