9

我正在监视一个 FTP 文件夹中的订单,然后在删除文件之前将其读入字符串数组以进行进一步处理。

总的来说是可行的,但偶尔我会遇到以下异常:

FileStream 被要求打开一个不是文件的设备。要支持“com1:”或“lpt1:”等设备,请调用 CreateFile,然后使用将操作系统句柄作为 IntPtr 的 FileStream 构造函数。 Stacktrace - 在 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)
在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问,FileShare 共享,Int32 bufferSize,FileOptions 选项,字符串 msgPath,布尔 bFromProxy,布尔 useLongPath,布尔 checkHost)
在 System.IO.StreamReader..ctor( System.IO.StreamReader..ctor(String path, Encoding encoding) at System.IO.File.InternalReadAllLines(String path, Encoding encoding) 在 System.IO. .File.ReadAllLines(字符串路径)

但是,查看文件名和路径,我不希望抛出这样的错误(如 com1.txt)

\\xxx.xxx.xxx.xxx\mbftp\4392-24979.ORD

所有文件名的格式相同 [account]-[orderno].ORD,accounts 总是 4 位数字。

这是引发错误的代码:

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);
}

事实上它不是一个持续的错误是我无法解决的问题。我只是在上次访问文件后 2 秒才访问该文件,所以很确定它不是锁定的东西。它似乎显示在 30% 的订单文件正在处理中,70% 的工作没有错误。

4

0 回答 0