2

I'm currently copying the archive files of our oracle server, the one that i'm preventing to happen is while i'm copying the file server will have an error when it tried to update the file.

At first i tried

File.Copy(source, Destination, true);

But it seems like this method is locking the file, the server throw an error in the log:

OSD-04002: unable to open file
O/S-Error: (OS 32) The process cannot access the file because it is being used by another process.

I change my method to

FileStream rStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

But when i try to open the file using a test program with FileShare. Now it throws an error also that another process is locking the file.

Is it possible to let go or release the file once another process wants to access/write to it?

Akash KC
  • 16,057
  • 6
  • 39
  • 59
Tom
  • 19
  • 1
  • 3

1 Answers1

1

what about

var stream = System.IO.File.Open(
                 source, System.IO.FileMode.Open, System.IO.FileAccess.Read
             );

Does this lock the file?

The best solution would be to use VSS to prevent locking (your server is a windows box, right?)

  • create a snapshot
  • copy files from snapshot
  • delete snapshot

You can automate this with .NET

http://www.codeproject.com/Articles/22745/Volume-Shadow-Copies-from-NET

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • yes i tried the code you posted and it still locks the file, will try the VSS (but its a bit complicated) was thinking maybe there's another solution. BTW, I'm using filewatcher as files are created and modified, then i copy after the update. – Tom Jun 21 '12 at 09:40
  • An addition also the filewatcher and the copy program is running on a different server. The archive files from the oracle server is just a map drive readonly. can i run the vss on a map drive from another server? I guess i need to read more in detail. – Tom Jun 22 '12 at 04:14
  • This could be a good starting point http://technet.microsoft.com/en-us/library/ee923636.aspx Look at the link "Shadow Copies of Shared Folders", I haven't read further, but that sounds promissing. Any you can use WMI, which I suppose will work on remote servers, too: technet.microsoft.com/en-us/library/cc787108(v=ws.10).aspx – Jürgen Steinblock Jun 22 '12 at 08:58
  • thank you very much for the big help SchlaWiener, so far the FileShare.ReadWrite is working against the oracle archive files. It is not preventing to write on the file even my program is reading it (copying by byte) have monitored for couple of days now, if something comes up for sure i'll go to the VSS solution. I considere this close. – Tom Jun 27 '12 at 07:21