Why is .NET's File.Open with a UNC path making excessive SMB calls?

前端 未结 2 409
野性不改
野性不改 2021-02-01 02:29

I have a block of code that needs to open and read a lot of small text files from a NAS server using UNC paths. This code is part of a module that was originally written in C++

2条回答
  •  鱼传尺愫
    2021-02-01 03:11

    I don't really have a specific answer to why the .NET implementation is so chatty, but this behaviour would be due to the implementation of System.IO.FileStream as all that File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); is doing is passing the parameters to the FileStream constructor.

    public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
    {
        return new FileStream(path, mode, access, share);
    }
    

    Changing the behaviour of FileStream would mean that you would basically have to re-implement the FileStream class which will require a lot of effort.

    Your other more simpler alternative would be to create a native wrapper that calls the C++ code you gave. Then call the native wrapper from your C# code.

提交回复
热议问题