检查文件可访问性.NET Core

时间:2018-07-25 09:06:19

标签: c# file asp.net-core .net-core .net-standard

我想检查文件是否可访问。 我使用

列出文件
IFileProvider provider = new PhysicalFileProvider(_settings.appSettings.sourceDirectory);
var files = provider.GetDirectoryContents("");

然后我得到了 IFileInfo 不是FileInfo

的列表

我想检查文件是否未被使用或没有被扫描仪或类似工具创建(程序是目录扫描仪(位于扫描仪接收文件夹中),将这些文件发送到Web API)

在.NET 4.5中,我可以这样做:

public static bool IsFileLocked(this FileInfo file)
    {
        FileStream stream = null;

        try
        {
            stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
        }
        catch (IOException)
        {
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }

        return false;
    }

但是我无法在.NET Core 2或1中做到这一点

我可以使用流阅读器,但是我需要性能才能读取最多文件(此目录每30秒可以获取1000个文件)

谢谢

0 个答案:

没有答案