File in use

  • Thread starter Thread starter vul
  • Start date Start date
V

vul

I'm developing Windows Service which is going to listen MS Fax service
events and update the database when Fax Job changed its status.
I need to read OutboxLOG.txt which is used by MS Fax service.
I either would like to copy it and to work with its copy to use its data
for a database update or to copy the data from OutboxLOG.txt into another
file for further processing.
I'm using :
Do
Try
System.IO.File.Copy(strSource, strDestination, True)
Exit Do
Catch
End Try
Loop

In most cases it works, but sometimes (I have no idea what it depends on) it
doesn't. I suspect that the source file is in use and is not copied.
Could anybody suggest any other approach to copy file when it's in use, or
to copy its contents?

Thank you
Al
 
vul said:
I'm developing Windows Service which is going to listen MS Fax service
events and update the database when Fax Job changed its status.
I need to read OutboxLOG.txt which is used by MS Fax service.
I either would like to copy it and to work with its copy to use its data
for a database update or to copy the data from OutboxLOG.txt into another
file for further processing.
I'm using :
Do
Try
System.IO.File.Copy(strSource, strDestination, True)
Exit Do
Catch
End Try
Loop

In most cases it works, but sometimes (I have no idea what it depends on) it
doesn't. I suspect that the source file is in use and is not copied.
Could anybody suggest any other approach to copy file when it's in use, or
to copy its contents?

Thank you
Al

If the fax service is writing to the log at that moment you might be
screwed. You could try waiting a few seconds and trying it again. If
you copy it though you may lock the file while the fax program tries to
write to it. It would probably be better if you opened it w/ shared
permission and read it, then wrote it out.

Chris
 
How to open the file w/ shared permission and read it? Unfortunately I have
no idea

Thank you
Al
 
I have both on the server. Initially I developed Windows Service in VS 2003.
But I tried to convert it to VS 2005 and tested. I did not expect any
difference in the service behavior and there was no difference.
The same failures sometime.

Thank you
Al
 
Hello Vul

This should work.

byte[] bytesRead = new byte[maxBytes];
long previousSeekPosition = 0;
int maxBytes = 4096;

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite );
if (fs.Length > maxBytes)
{

this.previousSeekPosition = fs.Length - maxBytes;

}
this.previousSeekPosition = (int)fs.Seek(this.previousSeekPosition,
SeekOrigin.Begin);
int numBytes = fs.Read(bytesRead, 0, maxBytes);
fs.Close();
this.previousSeekPosition += numBytes;

StringBuilder sb = new StringBuilder();
for (int i=0; i<numBytes; i++)
{

sb.Append((char)bytesRead);

}

// Your data will then be in sb.ToString();

Regards,
Simon
 
Back
Top