Windows service stops when deleting a file

22 Views Asked by At

This is a windows service. I have 2 files in the fileList object in the code mentioned below. So when the for loop is processed then the first loop works ok i.e. the file gets deleted as intended. But when it comes to the 2nd loop then exactly at the point of deleting i.e. File.Delete(file.FileName), the windows service stops. It does not even go into the catch block. The code is working properly on my local machine. But on the server the windows service stops as mentioned above.

Following is the simplified version of my code...


try
{
  foreach (var edpItem in edpAmalgamateBatchList)
  { 
    NPSLogger.Log.Error("before if...");
    if (File.Exists(Path.Combine(edpItem.FILE_LOCATION, edpItem.JOB_REFERENCE.Replace("\\", "_").Replace("/", "_").Replace(":", "_") + ".PDF")))
    {
      NPSLogger.Log.Debug("inside if");
      File.Delete(Path.Combine(edpItem.FILE_LOCATION, edpItem.JOB_REFERENCE.Replace("\\", "_").Replace("/", "_").Replace(":", "_") + ".PDF"));
      NPSLogger.Log.Debug(" file deleted.");
    }
  }
}
catch (Exception ex)
{
  NPSLogger.Log.Error("Error - ", ex); 
  return false;
}
                    

I also tried putting a sleep(1 second) in between each loop, but that too didnt help.

UPDATE

I tried putting a sleep of 1 minute instead of 1 second and it worked properly. I thought perhaps the file is being locked. So I tried the following to check if the file is locked... and if locked then put a sleep of 3 seconds and retry it. I do this retry attempts 5 times in a loop... see the following updated code which I tried... but it seems that the file is not locked because the retry loop bit only worked once and it did not go into the exception block of the IsFileLocked method...

try
{
  foreach (var edpItem in edpAmalgamateBatchList)
  {
    NPSLogger.Log.Debug("before if - " + edpItem.JOB_REFERENCE);
    NPSLogger.Log.Error("before if...");
    if (File.Exists(Path.Combine(edpItem.FILE_LOCATION, edpItem.JOB_REFERENCE.Replace("\\", "_").Replace("/", "_").Replace(":", "_") + ".PDF")))
    {
      NPSLogger.Log.Debug("inside if");
      FileInfo filetoDelete = new FileInfo(Path.Combine(edpItem.FILE_LOCATION, edpItem.JOB_REFERENCE.Replace("\\", "_").Replace("/", "_").Replace(":", "_") + ".PDF"));
      for (int tries = 0; IsFileLocked(filetoDelete) && tries < 5; tries++)
     Thread.Sleep(3000);

   File.Delete(Path.Combine(edpItem.FILE_LOCATION, edpItem.JOB_REFERENCE.Replace("\\", "_").Replace("/", "_").Replace(":", "_") + ".PDF"));

NPSLogger.Log.Debug(edpItem.JOB_REFERENCE.Replace("\\", "_").Replace("/", "_").Replace(":", "_") + ".PDF" + " file deleted.");
     }
  }
}
catch (Exception ex)
{
  NPSLogger.Log.Error("Error - ", ex);
  return false;
}


///This method to check if the file is locked or not
protected virtual bool IsFileLocked(FileInfo file)
{
  FileStream stream = null;
  NPSLogger.Log.Debug("In IsFileLocked.");
  try
  {
    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  }
  catch (IOException)
  {
    NPSLogger.Log.Debug("File is Locked.");
    //the file is unavailable because it is:
    //still being written to
    //or being processed by another thread
    //or does not exist (has already been processed)
    return true;
  }
  finally
  {
    if (stream != null)
    stream.Close();
  }
  NPSLogger.Log.Debug("File is not Locked.");
  //file is not locked
  return false;
}

0

There are 0 best solutions below