//创建缓存文件 //文件将以随机的Guid命名,完成写入时再命名为期望的文件名 //此举有助于防止写入一半时程序非法终止而产生损坏的缓存文件 public static async Task <TempStorageFile?> CreateCacheFileAsync(string Filename) { Guid guid; var test = Guid.TryParse(Filename, out guid); //避免使用GUID作为文件名 if (test) { return(null); } do { guid = Guid.NewGuid(); }while (await GetCachedFileAsync(guid.ToString()) != null); var cacheFolder = await getCacheFolderAsync(); var newFile = await cacheFolder.CreateFileAsync(guid.ToString()); TempStorageFile result = new TempStorageFile(); result.ExpectedName = Filename; result.File = newFile; return(result); }
//完成写入缓存文件 //ForceUpdate: 是否覆盖已有的文件 public static async Task <bool> FinishCachedFileAsync(TempStorageFile File, bool ForceUpdate = false) { try { if (await GetCachedFileAsync(File.ExpectedName) != null) { if (ForceUpdate) { await DeleteCachedFileAsync(File.ExpectedName); } else { return(false); } } await File.File.MoveAsync(await getCacheFolderAsync(), File.ExpectedName); return(true); } catch { return(false); } }