我對WinRT開發平臺有點新了,它已經讓我發瘋了(我是一個長期的.Net開發人員,所有那些被刪除的API都很煩人) I'當壓縮存在於Windows.Storage.ApplicationData.Current.TemporaryFolder中的所有文件時遇到問題。這裏是我當前的代碼(VB.Net,基於MSDN代碼,「file」是zip文件,我將把所有的文件文件到):對文件夾中的所有文件進行壓縮
Using zipMemoryStream As New MemoryStream()
Using zipArchive As New Compression.ZipArchive(zipMemoryStream, Compression.ZipArchiveMode.Create)
For Each fileToCompress As Windows.Storage.StorageFile In (Await Windows.Storage.ApplicationData.Current.TemporaryFolder.GetFilesAsync())
Dim buffer As Byte() = WindowsRuntimeBufferExtensions.ToArray(Await Windows.Storage.FileIO.ReadBufferAsync(fileToCompress))
Dim entry As ZipArchiveEntry = zipArchive.CreateEntry(fileToCompress.Name)
Using entryStream As Stream = entry.Open()
Await entryStream.WriteAsync(buffer, 0, buffer.Length)
End Using
Next
End Using
Using zipStream As Windows.Storage.Streams.IRandomAccessStream = Await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)
Using outstream As Stream = zipStream.AsStreamForWrite()
Dim buffer As Byte() = zipMemoryStream.ToArray()
outstream.Write(buffer, 0, buffer.Length)
outstream.Flush()
End Using
End Using
End Using
它建立好了,但是當我啓動代碼,我有例外: UnauthorizedAccessException:拒絕訪問。 (例外德HRESULT:0X80070005(E_ACCESSDENIED)) 在線:?!WindowsRuntimeBufferExtensions.ToArray(blahblah ... 我不知道什麼是錯的任何想法
在此先感謝
非常感謝!我注意到我忘記關閉文件流,這讓Windows認爲它們仍然在使用。 – user1857763