I'm monitoring an FTP folder for orders which I then read into string array for further processing before deleting the file.
On the whole is works but occasionally I get the following exception:
FileStream was asked to open a device that was not a file. For support for devices like 'com1:' or 'lpt1:', call CreateFile, then use the FileStream constructors that take an OS handle as an IntPtr.. Stacktrace - at Microsoft.Win32.Win32Native.SafeCreateFile(String lpFileName, Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) at System.IO.StreamReader..ctor(String path, Encoding encoding) at System.IO.File.InternalReadAllLines(String path, Encoding encoding) at System.IO.File.ReadAllLines(String path)
However, looking at the filename and path it's nothing that I'd expect to throw such an error (like com1.txt)
\\xxx.xxx.xxx.xxx\mbftp\4392-24979.ORD
All filenames are in the same format [account]-[orderno].ORD, accounts are always 4 digit number.
Here is the code that throws the error:
try
{
if (Directory.Exists(GetElementValue("FTPOrderFolder")))
{
if (File.Exists(fullOrderFilename))
{
if (File.GetLastAccessTime(fullOrderFilename) < DateTime.Now.AddSeconds(-2))
{
order = File.ReadAllLines(fullOrderFilename);
if (order.Length > 0)
{
File.Delete(fullOrderFilename);
NLogHelper.Debug(this, $"Deleted order file : {fullOrderFilename}");
return order;
}
}
}
}
}
catch (Exception ex)
{
NLogHelper.HandledException(this, ex);
}
The fact it's not a constant error is the thing I can't work out. I'm only accessing the file 2 seconds after it's last accessed so pretty sure it not a lock thing. It seems to show on 30% of order files being processed, the 70% working without an error.