/// <summary> /// Test if a file exists in the application local cache folder. /// </summary> /// <param name="knownFolderId">The well known folder ID to use</param> /// <param name="fileName">Relative name of the file to open. Can contains subfolders.</param> /// <returns>True if file exists.</returns> public static Task <bool> IsKnownFolderFileExistsAsync( KnownFolderId knownFolderId, string fileName) { StorageFolder workingFolder = StorageFileHelper.GetFolderFromKnownFolderId(knownFolderId); return(workingFolder.IsFileExistsAsync(fileName)); }
/// <summary> /// Return a stream to a specified file from the application local cache folder. /// </summary> /// <param name="knownFolderId">The well known folder ID to use</param> /// <param name="fileName">Relative name of the file to open. Can contains subfolders.</param> /// <param name="accessMode">File access mode. Default is read.</param> /// <returns>File stream</returns> public static Task <IRandomAccessStream> GetKnowFoldersFileStreamAsync( KnownFolderId knownFolderId, string fileName, FileAccessMode accessMode = FileAccessMode.Read) { StorageFolder workingFolder = StorageFileHelper.GetFolderFromKnownFolderId(knownFolderId); return(GetFileStreamAsync(fileName, accessMode, workingFolder)); }
/// <summary> /// Gets an array of bytes from a <see cref="StorageFile"/> located in the given <see cref="StorageFolder"/>. /// </summary> /// <param name="fileLocation"> /// The <see cref="StorageFolder"/> to save the file in. /// </param> /// <param name="fileName"> /// The relative <see cref="string"/> file path. /// </param> /// <returns> /// Returns the stored <see cref="byte"/> array. /// </returns> /// <exception cref="ArgumentNullException"> /// Exception thrown if the <paramref name="fileName"/> is null or empty. /// </exception> public static async Task <byte[]> ReadBytesFromFileAsync( this StorageFolder fileLocation, string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { throw new ArgumentNullException(nameof(fileName)); } var file = await fileLocation.GetFileAsync(fileName); return(await StorageFileHelper.ReadBytesAsync(file)); }
/// <summary> /// Save object inside file. /// There is no limitation to use this method (refers to <see cref="Save{T}(string, T)"/> method for simple objects). /// </summary> /// <typeparam name="T">Type of object saved</typeparam> /// <param name="filePath">Path to the file that will contain the object</param> /// <param name="value">Object to save</param> /// <returns>When this method completes, it returns the <see cref="StorageFile"/> where the object was saved</returns> public Task <StorageFile> SaveFileAsync <T>(string filePath, T value) { return(StorageFileHelper.WriteTextToFileAsync(Folder, JsonConvert.SerializeObject(value), filePath, CreationCollisionOption.ReplaceExisting)); }
/// <summary> /// Retrieve object from file. /// </summary> /// <typeparam name="T">Type of object retrieved</typeparam> /// <param name="filePath">Path to the file that contains the object</param> /// <param name="default">Default value of the object</param> /// <returns>Waiting task until completion with the object in the file</returns> public async Task <T> ReadFileAsync <T>(string filePath, T @default = default(T)) { string value = await StorageFileHelper.ReadTextFromFileAsync(Folder, filePath); return((value != null) ? JsonConvert.DeserializeObject <T>(value) : @default); }