コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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));
        }
コード例 #3
0
ファイル: WebDataCache.cs プロジェクト: obfan/FanfouUWP
        /// <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));
        }
コード例 #4
0
ファイル: WebDataCache.cs プロジェクト: obfan/FanfouUWP
        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);
        }
コード例 #5
0
        /// <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;
            }
        }
コード例 #6
0
        /// <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);
            }
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
ファイル: StorageHelper.cs プロジェクト: sanjayasl/Q42.WinRT
        /// <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;
            }
        }
コード例 #9
0
        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);
                }
            }
        }
コード例 #10
0
ファイル: UpdateTask.cs プロジェクト: mikaeldui/MinaLaromedel
        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();
        }
コード例 #11
0
ファイル: StorageHelper.cs プロジェクト: sanjayasl/Q42.WinRT
        /// <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;
            }
        }