Exclusive file access

C

cubaman

Hello:
Im workin with an app that checks for incoming files to a given folder
using a FileSystemWatcher 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(fileName, FileMode.Open,
FileAccess.Read, FileShare.None);
strm.Close();
strm.Dispose();
inUse = false;
}
catch (IOException) {
inUse = true;
LogHelper.WriteLog("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
FileSystemWatcher 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
 
M

Mr. Arnold

cubaman said:
Hello:
Im workin with an app that checks for incoming files to a given folder
using a FileSystemWatcher 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
FileSystemWatcher 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.
 
C

cubaman

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
 
C

cubaman

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 CampaignReceiverService {
public static class NativeMethods {
private const uint GENERIC_WRITE = 0x40000000;
private const uint OPEN_EXISTING = 3;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateFile(string lpFileName,
uint dwDesiredAccess,
uint dwShareMode,
IntPtr
lpSecurityAttributes,
uint
dwCreationDisposition,
uint
dwFlagsAndAttributes,
IntPtr hTemplateFile);

[DllImport("kernel32.dll", SetLastError = true)]
[return : MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr hObject);


public static bool IsFileInUse(string path) {
IntPtr fileHandle;
fileHandle = CreateFile(path, GENERIC_WRITE, 0,
IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (fileHandle.ToInt32() == -1) {
return true;
}
else {
CloseHandle(fileHandle);
return false;
}
}
}
}

Best regards.

Oscar Acosta
mcad
 

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

Top