Caching a binary file in C#

后端 未结 5 443
猫巷女王i
猫巷女王i 2021-01-02 22:07

Is it possible to cache a binary file in .NET and do normal file operations on cached file?

5条回答
  •  半阙折子戏
    2021-01-02 22:45

    Well, you can of course read the file into a byte[] array and start working on it. And if you want to use a stream you can copy your FileStream into a MemoryStream and start working with it - like:

    public static void CopyStream( Stream input, Stream output )
    {
            var buffer = new byte[32768];
            int readBytes;
            while( ( readBytes = input.Read( buffer, 0, buffer.Length ) ) > 0 )
            {
                    output.Write( buffer, 0, readBytes );
            }
    }
    

    If you are concerned about performance - well, normally the build-in mechanisms of the different file access methods should be enough.

提交回复
热议问题