要求FileStream打开不是文件的设备

时间:2018-03-16 07:07:37

标签: c# filestream file.readalllines

我监控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   访问,Int32权限,布尔useRights,FileShare共享,Int32   bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String   msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)
  在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess   访问,FileShare共享,Int32 bufferSize,FileOptions选项,字符串   msgPath,Boolean bFromProxy,Boolean useLongPath,Boolean checkHost)
  在System.IO.StreamReader..ctor(字符串路径,编码编码,   Boolean detectEncodingFromByteOrderMarks,Int32 bufferSize,Boolean   checkHost)在System.IO.StreamReader..ctor(String path,Encoding。)   编码)在System.IO.File.InternalReadAllLines(String path,   在System.IO.File.ReadAllLines(String path)

编码)

但是,查看文件名和路径,我没想到会发生这样的错误(如com1.txt)

  

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

所有文件名格式相同[account] - [orderno] .ORD,帐号始终为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%的工作没有错误。

0 个答案:

没有答案
相关问题