1

I have a Windows service running on Windows Server 2008 R2 Standard Edition. It's a .NET 4.0 application which processes binary files and moves processed files to a different folder on the same volume. File system is NTFS. The issue is with the following code:

try
{
    if (File.Exists(srcFileName))
    {
        File.Move(srcFileName, dstFileName);
    }
}
catch (Exception ex)
{
    log.Error("Failed to move file.", ex);
}

Paths srcFileName and dstFileName have following forms: D:\src_dir\fileX.bin and D:\dst_dir\fileX.bin. The log is log4net logger object. Files are read for processing with File.ReadAllBytes(srcFileName).

The problem is that although code works as expected most of the time, occasionally some files are copied instead of being moved to the destination folder and there are no errors recorded in the log. Looking through the log it seems that everything works fine, but some files appear in both source and destination folders. It appears that the issue is related to server load, as it seems to happen when RAM usage goes over 30 GB (from available 32 GB) and average disk queue goes over 2.

I would appreciate any idea what might cause this behavior, especially absence of exception when move fails. Is there a way to be sure that move operation completed successfully?

elojd
  • 11
  • 3
  • Is it always the same folders and the same users or is the problem with specific users or folders only? – Jonny Aug 16 '16 at 15:10
  • @Jonny It is always the same user and the same two folders. The service is running under local system account. – elojd Aug 16 '16 at 15:15

1 Answers1

0

Offhand, it appears that the delete operation is failing.

There are two ways to "move" a file: actually move the directory information, and--barring that being possible--bodily copying it to the destination and then deleting the target. In the instances that you describe, option (2) was resorted to and the deletion attempt failed for whatever reason. Perhaps the file was open for I/O by another process or application (not a problem on UNIX: it's O.K. to drop directory entries while the i-node is active, since current readers/writers don't need the directory entry once they have obtained a valid file descriptor thereto).

  • If I understand `File.Move()` correctly, it should not perform copy+delete when used within the same volume. I would expect exception to be thrown if the operation could not be completed successfully. – elojd Aug 17 '16 at 17:05