我正在尝试读取标签,更改标签,然后将数据写回同一文件。读取标签没有问题,但写入不起作用。数据似乎没有保存到磁盘。
我的测试用例。
var file = await KnownFolders.MusicLibrary.GetFileAsync("sampleUnderTest.wav");
var content = await ReadFileAsynch(file);
content.Add("newTag","newValue")
await SubmittFileAsynch(file, content)
var newContent = await ReadFileAsynch(file);
Assert.NotEqual(content,newContent)
Assert会抛出异常,因为content和newContent是相同的。
这是我的代码的一部分:
public async Task SubmittFileAsynch(StorageFile file, Content content)
{
var accessStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (accessStream)
await CommitAsynch(accessStream, content);
}
internal async Task CommitAsynch(IRandomAccessStream accessStream, Content content)
{
List<byte[]> tbw = new List<byte[]>();
using (var InStream = accessStream.AsStreamForRead())
{
using (var br = new BinaryReader(InStream))
{
//Reads file until it reaches the part of the containing tags and
//that are going to be modified by content.
//tbw is created from data in content and from the read process.
await BuildAsync(br, content, tbw);
using (var outstream = (accessStream.GetOutputStreamAt(0)))
using (var bw = new DataWriter(outstream))
{
await CommitAsync(tbw, bw);
await bw.FlushAsync();
}
}
}
}
private async Task CommitAsync(List<byte[]> tbw, DataWriter bw)
{
await Task.Run(() =>
{
foreach (byte[] buf in tbw)
bw.WriteBytes(buf);
});
}
我看不出我做错了什么,并希望得到一些帮助。
// LG
答案 0 :(得分:0)
我回来了微笑
最后我解决了我的问题。我的代码中有一些线程问题。我将写入部分更改为:
using (var bw = new DataWriter(outstream))
{
//await CommitAsync(tbw, bw);
foreach (byte[] buf in tbw)
bw.WriteBytes(buf);
await bw.StoreAsync();
bw.DetachStream();
}
await outstream.FlushAsync();
现在效果很好。我不确定是否有一些冗余的代码,对于&#34;使用&#34;子句。
如果有人能给我一些关于fileIO,stream等的好读物,我会很高兴。我在互联网上发现的一切都非常基本,
希望这会帮助别人。