文件和流的相互转化

 1 public class UploadFile
 2 {
 3     //文件名称
 4     public string FileName{get;set;}
 5     //文件类型
 6     public StorageFile File{get;set;}
 7     
 8     //文件数据流
 9    public Stream FileStream { get; private set; }
10    
11    
12     public UploadFile(StorageFile file)
13     {
14         this.StorageFile=file.Name;
15         this.File=file;
16     }
17     
18     //将文件转换成流
19     public async Task<byte[]> GetBytes()
20     {
21         FileStream =(Stream) await File.OpenStreamForReadAsync();
22         byte[] bytes = new byte[FileStream.Length];
23         FileStream.Read(bytes, 0, bytes.Length);
24         FileStream.Seek(0, SeekOrigin.Begin);
25         return bytes;
26     }
27     
28     
29     
30     //将流写入文件
31        public async void GetFile(Byte[] bytes)
32         { 
33             StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
34             StorageFile storageFile = await applicationFolder.CreateFileAsync("1.txt", CreationCollisionOption.ReplaceExisting);
35             StorageFile file = await applicationFolder.GetFileAsync("1.txt");
36             using (var stream = await file.OpenStreamForWriteAsync())
37             {
38                 await stream.WriteAsync(bytes, 0, bytes.Length);
39                 stream.Flush();
40             }
41 
42     
43 }

 

posted @ 2015-07-27 20:34  某只菜鸟  阅读(1092)  评论(0编辑  收藏  举报