Exemple #1
0
        /// <summary>
        /// Retrieves a StorageFile from a given StorageFolderLocation with a given file name. Optional parameter to create the file if it doesn't exist.
        /// </summary>
        /// <param name="location">
        /// The StorageFolderLocation.
        /// </param>
        /// <param name="fileName">
        /// The file name.
        /// </param>
        /// <param name="createIfNotExists">
        /// Whether to create the file if it doesn't exist.
        /// </param>
        /// <returns>
        /// Returns the StorageFile, if exists.
        /// </returns>
        public async Task<StorageFile> RetrieveStorageFileAsync(StorageFolderLocation location, string fileName, bool createIfNotExists)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            var folder = await this.RetrieveStorageFolderAsync(location);

            var file = await GetFileAsync(folder, fileName);
            if (file != null || !createIfNotExists)
            {
                return file;
            }

            file = await folder.CreateFileAsync(fileName, CreationCollisionOption.OpenIfExists);

            return file;
        }
Exemple #2
0
        /// <summary>
        /// Retrieves a StorageFolder for a given StorageFolderLocation.
        /// </summary>
        /// <param name="location">
        /// The StorageFolderLocation to find.
        /// </param>
        /// <returns>
        /// Returns the StorageFolder.
        /// </returns>
        public async Task<StorageFolder> RetrieveStorageFolderAsync(StorageFolderLocation location)
        {
            var folder = ApplicationData.Current.LocalFolder;
            switch (location)
            {
                case StorageFolderLocation.Root:
                    break;
                case StorageFolderLocation.User:
                    folder = await folder.CreateFolderAsync(UserStorageFolderName, CreationCollisionOption.OpenIfExists);
                    break;
                case StorageFolderLocation.Logs:
                    folder = await folder.CreateFolderAsync(LogsStorageFolderName, CreationCollisionOption.OpenIfExists);
                    break;
                case StorageFolderLocation.Temp:
                    folder = ApplicationData.Current.TemporaryFolder;
                    break;
            }

            return folder;
        }