Hello,
I use the winscp .net library 5.15.4 to send files to a server.
At the end of a transfer I attempt to archive the respective transferred file (i.e. put it in zip file), however I started to get occasional "System.IO.IOException: The process cannot access the file" exceptions.
My code is very simple:
protected override void ExecuteTransfer(IFileInfo fi)
{
TransferOptions transferOptions = new TransferOptions
{
TransferMode = TransferMode.Binary,
OverwriteMode = OverwriteMode.Overwrite,
FilePermissions = null, // This is default
PreserveTimestamp = false
};
string destinationFile = $"{_ts._transferPipe.RemoteFolder}/{fi.Name}";
_ts._session.PutFiles(@fi.FullName, destinationFile, false, transferOptions).Check();
// If it was successful - move the file to processed
// Archive won't throw an exception
Archive(fi.FullName);
}
private void Archive(string fileFullName)
{
FileInfo fileInfo = new FileInfo(fileFullName);
try
{
string zipFileName = $"{_ts._transferPipe.ProcessedFolder}\\archive_{DateTime.Now:yyyyMM}.zip";
using (var zipArchive = ZipFile.Open(zipFileName, ZipArchiveMode.Update))
{
zipArchive.CreateEntryFromFile(fileInfo.FullName, fileInfo.Name);
}
if (fileInfo.Exists)
fileInfo.Delete();
}
catch (Exception ex)
{
_log.Error($"An error occurred while trying to archive or delete the file {fileFullName}. I am going to attempt to move it.", ex);
string destFileName = $@"{_ts._transferPipe.ProcessedFolder}\{fileInfo.Name}";
try
{
if (fileInfo.Exists)
fileInfo.MoveTo(destFileName);
}
catch (Exception ex2)
{
_log.Error($"Moving the file {fileFullName} to {destFileName} failed as well! I will leave it here!", ex2);
}
}
}
The archive method fails, then moving the file fails as well. Could it be that WinScp still locks the file after the transfer? The other possibility would be that the antivirus software locks the file (Trend Micro) but the more I think about it I think it is WinScp. I investigated with Resource Monitor to see what process locked the file, however, it was too late, the lock was released.
I also started to look at the ResumeSupport property. I am not clear how this works. As far as I am concerned if a transfer fails my app is going to retry to send it. Should I set the ResumeSupport to Off?
Thanks!