Handling locked files with C#

M

Markus Erlacher

Hi Newsgroup,

I have a problem that is not directly C#-related, but I'd like to
solve it in C#.

A data acquisition station generates heaps of data, which shall be
transferred to a server. As the connection may be rather slow, I can't
wait until the acquisition ends - and unfortunately I don't have
access to the data acquisition source codes.
Is there a way to transfer this data while it is being recorded? The
acquisition program locks the files during acquiring and writing the
data, so opening the files and transferring the data that is already
there, probably won't work. But there are programs out there that can
open (for reading) locked files. How are they doing it?
I thought, maybe Volume Shadow Services might be a way, but I could
not find any examples proving it.

Any ideas?

Regards
Markus
 
V

Vadym Stetsiak

Hello, Markus!

File is locked both for reading and writing?
Did you try opening the file with, say notepad. Will it succeed?

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Tue, 16 Oct 2007 09:17:49 -0000:

ME> Hi Newsgroup,

ME> I have a problem that is not directly C#-related, but I'd like to
ME> solve it in C#.

ME> A data acquisition station generates heaps of data, which shall be
ME> transferred to a server. As the connection may be rather slow, I
ME> can't wait until the acquisition ends - and unfortunately I don't
ME> have access to the data acquisition source codes.
ME> Is there a way to transfer this data while it is being recorded? The
ME> acquisition program locks the files during acquiring and writing the
ME> data, so opening the files and transferring the data that is already
ME> there, probably won't work. But there are programs out there that
ME> can open (for reading) locked files. How are they doing it?
ME> I thought, maybe Volume Shadow Services might be a way, but I could
ME> not find any examples proving it.

ME> Any ideas?

ME> Regards
ME> Markus
 
M

Markus Erlacher

Hi Vadym,

Opening it with Notepad fails, drag-n-drop to make a copy fails also.

regards
Markus
 
W

Willy Denoyette [MVP]

Markus Erlacher said:
Hi Vadym,

Opening it with Notepad fails, drag-n-drop to make a copy fails also.

Which means that the file is opened in *exclusive* mode, there is nothing
you can do about this, no other program can open such file simultaneously.
As you don't own the acquisition source code, VSS is not an option either.

Willy.
 
M

Markus Erlacher

Hi Willy,

thanks for the response. That's no good news for me, but probably in
terms of security the right thing for the operating system to do. But
thanks for the hint that even VSS is no option.
But one thing still confuses me: how do backup softwares handle these
files?

Markus
 
W

Willy Denoyette [MVP]

Markus Erlacher said:
Hi Willy,

thanks for the response. That's no good news for me, but probably in
terms of security the right thing for the operating system to do. But
thanks for the hint that even VSS is no option.
But one thing still confuses me: how do backup softwares handle these
files?

Markus



Basically you need to open the file using Win32 "CreateFile" API with:
dwDesiredACcess = 0
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE
and dwFlagsAndAttributes = FILE_FLAG_BACKUP_SEMANTICS set.
Once you have the file handle, you can use it to perform backup operations
using the Backup API's like Kernel32 API "BackupRead".
Nothing of this all is wrapped by the Framework, you need to PInvoke. Also,
you need to run with the "SeBackupPrivilege "enabled for your process, (IMO)
something you don't want your user processes to run with.


Willy.
 

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