通过二进制 reader 读取 mp3 文件时出现 C# 错误:进程无法访问文件 'URL\testbinary.mp3',因为它正被另一个进程使用

C# error on reading mp3 file by binary reader : The process cannot access the file 'URL\testbinary.mp3' because it is being used by another process

嘿嘿, 我想用二进制 reader 读取 mp3 文件,我的代码是:

using (BinaryReader br = new BinaryReader(File.Open("Songs/testbinary.mp3", FileMode.Open)))
        {
           int length = (int)br.BaseStream.Length;

            byte[] bytes = br.ReadBytes(length);

            txtBinary.Text = bytes.ToString();
        }

....... 当我执行此代码时,它显示异常:

The process cannot access the file 'URL\testbinary.mp3' because it is being used by another process.

其中 "URL" 是我的实际文件位置。

您打开同一个文件两次(没有任何共享选项)。要以字节形式读取文件内容,您可以使用 File.ReadAllBytes

byte[] bytes = File.ReadAllBytes("Songs/testbinary.mp3");

顺便说一句:不要忘记 txtBinary.Text = bytes.ToString(); 不会给你你的想法。您将不得不使用 BitConverter.ToStringConvert.ToBase64String