Windows Phone 7 中拷贝文件到独立存储

时间:2023-03-09 18:47:27
Windows Phone 7 中拷贝文件到独立存储

private void CopyToIsolatedStorage()
{
    using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        string[] files = new string[] { "下雨天.mp3", "用心听.mp3", "我们的歌.mp3" };

foreach (var _fileName in files)
        {
            if (!storage.FileExists(_fileName))
           {
               string _filePath = "Audio/" + _fileName;
               StreamResourceInfo resource = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative));

using (IsolatedStorageFileStream file = storage.CreateFile(_fileName))
                 {
                          int chunkSize = 4096;
                        byte[] bytes = new byte[chunkSize];
                      int byteCount;

while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
                     {
                          file.Write(bytes, 0, byteCount);
                     }
                 }
           }
       }
    }
}