var outputfile = "outputfile.aac";
var fileList = new List<string>
{
"1.aac",
"2.aac",
"3.aac",
"4.aac",
"5.aac",
"6.aac"
};
foreach (var file in fileList)
{
using (var stream = new FileStream(outputfile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
var bytes = File.ReadAllBytes(file);
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
}
}
我尝试将fileList中的aac文件加入到outputfile中。当将文件“3.aac”写入outputfile.aac时,我用MediaPlayer打开outputfile,然后FileStream返回异常“正在使用的文件”,即使FileShare模式是ReadWrite,也意味着其他进程可以读写该文件。那么原因在哪里?
但是,在这种情况下,输出文件不会被阻止
Stream s = resp.GetResponseStream();
var fs = File.Exists(outputFile) ? new FileStream(outputFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)
: new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
byte[] buffer = new byte[4096];
while (s.CanRead)
{
int bytesRead = s.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
}
P / s:我发现原因是因为它是aac文件
答案 0 :(得分:2)
当您使用FileShare.ReadWrite
时,您允许在读取或写入模式下随后打开文件。
MediaPlayer然后打开FileShare.Read
侧的文件,拒绝您对该文件的写入权限。