/// <summary> /// Creates a unique file name given the parameters. /// Note the theoretical race condition since /// the file name is only guaranteed to be unique at the point it is generated. /// </summary> /// <param name="folder"></param> /// <param name="extension"></param> /// <param name="prefix"></param> /// <param name="suffix"></param> /// <returns></returns> public static async Task <string> CreateTempFileNameAsync( this StorageFolder folder, string extension = ".tmp", string prefix = "", string suffix = "") { string fileName; if (folder == null) { folder = ApplicationData.Current.TemporaryFolder; } if (string.IsNullOrEmpty(extension)) { extension = ".tmp"; } else if (extension[0] != '.') { extension = string.Format(".{0}", extension); } // Try no random numbers if (!string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(prefix)) { fileName = string.Format( "{0}{1}{2}", prefix, suffix, extension); if (!await folder.ContainsFileAsync(fileName)) { return(fileName); } } do { fileName = string.Format( "{0}{1}{2}{3}", prefix, Guid.NewGuid(), suffix, extension); } while (await folder.ContainsFileAsync(fileName)); return(fileName); }
/// <summary> /// Load object from file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public async Task <T> LoadAsync(string fileName) { fileName = fileName + FileExtension; StorageFolder folder = await GetFolderAsync().ConfigureAwait(false); if (await folder.ContainsFileAsync(fileName).ConfigureAwait(false)) { StorageFile file = await folder.GetFileAsync(fileName); string data; IRandomAccessStream accessStream = await file.OpenReadAsync(); if (accessStream.Size == 0) { return(default(T)); } using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size)) { var content = new byte[stream.Length]; await stream.ReadAsync(content, 0, (int)stream.Length).ConfigureAwait(false); data = Encoding.UTF8.GetString(content, 0, content.Length); } //Deserialize to object var result = JsonConvert.DeserializeObject <T>(data); return(result); } return(default(T)); }
/// <summary> /// Stores webdata in cache based on uri as key /// Returns local uri /// </summary> /// <param name="uri"></param> /// <returns></returns> public static async Task <Uri> GetLocalUriAsync(Uri uri) { //Ignore these uri schemes if (uri.Scheme == "ms-Resource" || uri.Scheme == "ms-appx" || uri.Scheme == "ms-appdata" || uri.Scheme == "isostore") { return(uri); } string key = uri.ToCacheKey(); //Try get the data from the cache StorageFolder folder = await GetFolderAsync().ConfigureAwait(false); bool contains = await folder.ContainsFileAsync(key).ConfigureAwait(false); if (!contains) { await SetAsync(uri).ConfigureAwait(false); } string localUri = string.Format("ms-appdata:///local/{0}/{1}", CacheFolder, key); return(new Uri(localUri)); }
public static async Task <StorageFile> GetAsync(Uri uri, bool forceGet = false) { string key = uri.ToCacheKey(); StorageFile file = null; //Try get the data from the cache StorageFolder folder = await GetFolderAsync().ConfigureAwait(false); bool exist = await folder.ContainsFileAsync(key).ConfigureAwait(false); //If file is not available or we want to force getting this file if (!exist || forceGet) { //else, load the data file = await SetAsync(uri).ConfigureAwait(false); } if (file == null) { file = await folder.GetFileAsync(key); } return(file); }
/// <summary> /// Load object from file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public async Task <T> LoadAsync(string fileName) { fileName = fileName + _fileExtension; try { StorageFile file = null; StorageFolder folder = await GetFolderAsync().ConfigureAwait(false); var contains = await folder.ContainsFileAsync(fileName).ConfigureAwait(false); if (contains) { file = await folder.GetFileAsync(fileName); var data = await FileIO.ReadTextAsync(file); //Deserialize to object T result = JsonConvert.DeserializeObject <T>(data); return(result); } else { return(default(T)); } } catch (Exception) { //Unable to load contents of file throw; } }
/// <summary> /// Delete file /// </summary> /// <param name="fileName"></param> public async Task DeleteAsync(string fileName) { fileName = fileName + FileExtension; StorageFolder folder = await GetFolderAsync().ConfigureAwait(false); if (await folder.ContainsFileAsync(fileName).ConfigureAwait(false)) { var file = await folder.GetFileAsync(fileName); await file.DeleteAsync(StorageDeleteOption.PermanentDelete); } }
/// <summary> /// Loads a BitmapImage asynchronously given a specific folder and file name. /// </summary> /// <param name="folder">The folder.</param> /// <param name="fileName">Name of the file.</param> /// <returns></returns> public static async Task <BitmapImage> LoadAsync(StorageFolder folder, string fileName) { BitmapImage bitmap = new BitmapImage(); if (await folder.ContainsFileAsync(fileName)) { var file = await folder.GetFileByPathAsync(fileName); return(await bitmap.SetSourceAsync(file)); } return(null); }
/// <summary> /// Load object from file /// </summary> /// <param name="fileName"></param> /// <returns></returns> public async Task <T> LoadAsync(string fileName) { fileName = fileName + GetFileExtension(); try { StorageFile file = null; StorageFolder folder = await GetFolderAsync().ConfigureAwait(false); var contains = await folder.ContainsFileAsync(fileName).ConfigureAwait(false); if (contains) { file = await folder.GetFileAsync(fileName); //Deserialize to object with JSON or XML serializer T result = default(T); switch (_serializerType) { case StorageSerializer.JSON: var data = await FileIO.ReadTextAsync(file); result = JsonConvert.DeserializeObject <T>(data); break; case StorageSerializer.XML: XmlSerializer serializer = new XmlSerializer(typeof(T)); IInputStream sessionInputStream = await file.OpenReadAsync(); result = (T)serializer.Deserialize(sessionInputStream.AsStreamForRead()); sessionInputStream.Dispose(); break; } return(result); } else { return(default(T)); } } catch (Exception ex) { //Unable to load contents of file throw; } }
public async void SetSound(string mp3) { if (mp3 != null) { StorageFolder Folder = Windows.ApplicationModel.Package.Current.InstalledLocation; Folder = await Folder.GetFolderAsync("Assets"); bool containsFile = await Folder.ContainsFileAsync(mp3); if (containsFile) { StorageFile sf = await Folder.GetFileAsync(mp3); MyMusic.AutoPlay = false; MyMusic.SetSource(await sf.OpenAsync(FileAccessMode.Read), sf.ContentType); } } }
public async void Run(IBackgroundTaskInstance taskInstance) { var deferral = taskInstance.GetDeferral(); // Credentials from settings to PasswordVault { IPropertySet LocalSettings = ApplicationData.Current.LocalSettings.Values; IPropertySet RoamingSettings = ApplicationData.Current.RoamingSettings.Values; if (LocalSettings.ContainsKey("username")) { var vault = new PasswordVault(); vault.Add(new PasswordCredential("Hermods Novo", (string)LocalSettings["username"], (string)LocalSettings["password"])); LocalSettings.Remove("username"); LocalSettings.Remove("password"); try { RoamingSettings.Remove("username"); RoamingSettings.Remove("password"); } catch { } } } // From 1.0.5.0 { const string FILE_NAME = "ebooksList.json"; StorageFolder StorageFolder = ApplicationData.Current.LocalFolder; var hermodsNovoEbooks = await StorageFolder.ContainsFileAsync(FILE_NAME) ? await StorageFolder.GetObjectAsync<HermodsNovoEbook[]>(FILE_NAME) : new HermodsNovoEbook[0]; if (hermodsNovoEbooks.Any(hne => hne.Url != null)) { var ebooks = hermodsNovoEbooks.Select(HermodsNovoEbookProvider.ConvertToEbook).ToArray(); await EbookStorage.SaveEbooksAsync(ebooks); } } deferral.Complete(); }
/// <summary> /// Delete file /// </summary> /// <param name="fileName"></param> public async Task DeleteAsync(string fileName) { fileName = fileName + GetFileExtension(); try { StorageFolder folder = await GetFolderAsync().ConfigureAwait(false); var contains = await folder.ContainsFileAsync(fileName).ConfigureAwait(false); if (contains) { var file = await folder.GetFileAsync(fileName); await file.DeleteAsync(StorageDeleteOption.PermanentDelete); } } catch (Exception) { throw; } }