コード例 #1
0
        /// <summary>Returns a file if it is found in the specified storage strategy</summary>
        /// <param name="key">Path of the file in storage</param>
        /// <param name="location">Location storage strategy</param>
        /// <returns>StorageFile</returns>
        private static async Task <Windows.Storage.StorageFile> GetIfFileExistsAsync(string key,
                                                                                     StorageStrategies location = StorageStrategies.Local,
                                                                                     Windows.Storage.CreationCollisionOption option = Windows.Storage.CreationCollisionOption.FailIfExists)
        {
            Windows.Storage.StorageFile retval;
            try
            {
                switch (location)
                {
                case StorageStrategies.Local:
                    retval = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.Roaming:
                    retval = await Windows.Storage.ApplicationData.Current.RoamingFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.Temporary:
                    retval = await Windows.Storage.ApplicationData.Current.TemporaryFolder.GetFileAsync(key);

                    break;

                default:
                    throw new NotSupportedException(location.ToString());
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                return(null);
            }

            return(retval);
        }
コード例 #2
0
        /// <summary>Returns a file if it is found in the specified storage strategy</summary>
        /// <param name="key">Path of the file in storage</param>
        /// <param name="location">Location storage strategy</param>
        /// <returns>StorageFile</returns>
        private async Task <StorageFile> GetIfFileExistsAsync(string key,
                                                              StorageStrategies location = StorageStrategies.Local)
        {
            StorageFile retval;

            try
            {
                switch (location)
                {
                case StorageStrategies.Local:
                    retval = await ApplicationData.Current.LocalFolder.TryGetItemAsync(key) as StorageFile;

                    break;

                case StorageStrategies.Roaming:
                    retval = await ApplicationData.Current.RoamingFolder.TryGetItemAsync(key) as StorageFile;

                    break;

                case StorageStrategies.Temporary:
                    retval = await ApplicationData.Current.TemporaryFolder.TryGetItemAsync(key) as StorageFile;

                    break;

                default:
                    throw new NotSupportedException(location.ToString());
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Diagnostics.Debug.WriteLine("GetIfFileExistsAsync:FileNotFoundException");
                return(null);
            }
            return(retval);
        }
コード例 #3
0
        private async Task <Windows.Storage.StorageFile> CreateFileAsync(string key, StorageStrategies location         = StorageStrategies.Local,
                                                                         Windows.Storage.CreationCollisionOption option = Windows.Storage.CreationCollisionOption.OpenIfExists)
        {
            switch (location)
            {
            case StorageStrategies.Local:
                return(await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(key, option));

            case StorageStrategies.Roaming:
                return(await Windows.Storage.ApplicationData.Current.RoamingFolder.CreateFileAsync(key, option));

            case StorageStrategies.Temporary:
                return(await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(key, option));

            case StorageStrategies.LocalCache:
                return(await Windows.Storage.ApplicationData.Current.LocalCacheFolder.CreateFileAsync(key, option));

            case StorageStrategies.SharedLocalFolder:
                return(await Windows.Storage.ApplicationData.Current.SharedLocalFolder.CreateFileAsync(key, option));

            case StorageStrategies.DocumentLibrary:
                return(await Windows.Storage.KnownFolders.DocumentsLibrary.CreateFileAsync(key, option));

            case StorageStrategies.PublisherCacheFolder:
                return(await Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("freistli.com").CreateFileAsync(key, option));

            default:
                throw new NotSupportedException(location.ToString());
            }
        }
コード例 #4
0
        /// <summary>Serializes an object and write to file in specified storage strategy</summary>
        /// <typeparam name="T">Specified type of object to serialize</typeparam>
        /// <param name="key">Path to the file in storage</param>
        /// <param name="value">Instance of object to be serialized and written</param>
        /// <param name="location">Location storage strategy</param>
        public static void SetSetting <T>(string key, T value, StorageStrategies location = StorageStrategies.Local)
        {
            switch (location)
            {
            case StorageStrategies.Local:
                if (SettingExists(key))
                {
                    Windows.Storage.ApplicationData.Current.LocalSettings.Values[key] = value;
                }
                else
                {
                    Windows.Storage.ApplicationData.Current.LocalSettings.Values.Add(key, value);
                }
                break;

            case StorageStrategies.Roaming:
                if (SettingExists(key))
                {
                    Windows.Storage.ApplicationData.Current.RoamingSettings.Values[key] = value;
                }
                else
                {
                    Windows.Storage.ApplicationData.Current.RoamingSettings.Values.Add(key, value);
                }
                break;

            default:
                throw new NotSupportedException(location.ToString());
            }
        }
コード例 #5
0
        public static T GetSetting <T>(string key, T otherwise    = default(T),
                                       StorageStrategies location = StorageStrategies.Local)
        {
            try
            {
                if (!(SettingExists(key, location)))
                {
                    return(otherwise);
                }

                switch (location)
                {
                case StorageStrategies.Local:
                    return((T)ApplicationData.Current.LocalSettings.Values[key]);

                case StorageStrategies.Roaming:
                    return((T)ApplicationData.Current.RoamingSettings.Values[key]);

                default:
                    throw new NotSupportedException(location.ToString());
                }
            }
            catch
            {
                return(otherwise);
            }
        }
コード例 #6
0
        /// <summary>Returns a file if it is found in the specified storage strategy</summary>
        /// <param name="key">Path of the file in storage</param>
        /// <param name="location">Location storage strategy</param>
        /// <returns>StorageFile</returns>
        public async Task <Windows.Storage.StorageFile> GetIfFileExistsAsync(string key,
                                                                             StorageStrategies location = StorageStrategies.Local,
                                                                             Windows.Storage.CreationCollisionOption option = Windows.Storage.CreationCollisionOption.FailIfExists)
        {
            Windows.Storage.StorageFile retval;
            try
            {
                switch (location)
                {
                case StorageStrategies.Local:
                    retval = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.Roaming:
                    retval = await Windows.Storage.ApplicationData.Current.RoamingFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.Temporary:
                    retval = await Windows.Storage.ApplicationData.Current.TemporaryFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.LocalCache:
                    retval = await Windows.Storage.ApplicationData.Current.LocalCacheFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.SharedLocalFolder:
                    retval = await Windows.Storage.ApplicationData.Current.SharedLocalFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.DocumentLibrary:
                    retval = await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync(key);

                    break;

                case StorageStrategies.PublisherCacheFolder:
                    retval = await Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("freistli.com").GetFileAsync(key);

                    break;

                default:
                    throw new NotSupportedException(location.ToString());
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Diagnostics.Debug.WriteLine("GetIfFileExistsAsync:FileNotFoundException");
                return(null);
            }

            return(retval);
        }
コード例 #7
0
        /// <summary>Returns if a setting is found in the specified storage strategy</summary>
        /// <param name="key">Path of the setting in storage</param>
        /// <param name="location">Location storage strategy</param>
        /// <returns>Boolean: true if found, false if not found</returns>
        public static bool SettingExists(string key, StorageStrategies location = StorageStrategies.Local)
        {
            switch (location)
            {
            case StorageStrategies.Local:
                return(Windows.Storage.ApplicationData.Current.LocalSettings.Values.ContainsKey(key));

            case StorageStrategies.Roaming:
                return(Windows.Storage.ApplicationData.Current.RoamingSettings.Values.ContainsKey(key));

            default:
                throw new NotSupportedException(location.ToString());
            }
        }
コード例 #8
0
        public static void DeleteSetting(string key, StorageStrategies location = StorageStrategies.Local)
        {
            switch (location)
            {
            case StorageStrategies.Local:
                Windows.Storage.ApplicationData.Current.LocalSettings.Values.Remove(key);
                break;

            case StorageStrategies.Roaming:
                Windows.Storage.ApplicationData.Current.RoamingSettings.Values.Remove(key);
                break;

            default:
                throw new NotSupportedException(location.ToString());
            }
        }
コード例 #9
0
        /// <summary>Serializes an object and write to file in specified storage strategy</summary>
        /// <typeparam name="T">Specified type of object to serialize</typeparam>
        /// <param name="key">Path to the file in storage</param>
        /// <param name="value">Instance of object to be serialized and written</param>
        /// <param name="location">Location storage strategy</param>
        public static void SetSetting <T>(string key, T value, StorageStrategies location = StorageStrategies.Local)
        {
            switch (location)
            {
            case StorageStrategies.Local:
                ApplicationData.Current.LocalSettings.Values[key.ToString()] = value;
                break;

            case StorageStrategies.Roaming:
                ApplicationData.Current.RoamingSettings.Values[key.ToString()] = value;
                break;

            default:
                throw new NotSupportedException(location.ToString());
            }
        }
コード例 #10
0
ファイル: FileHelper.cs プロジェクト: waynebaby/Template10
        private async Task <Windows.Storage.StorageFile> CreateFileAsync(string key, StorageStrategies location         = StorageStrategies.Local,
                                                                         Windows.Storage.CreationCollisionOption option = Windows.Storage.CreationCollisionOption.OpenIfExists)
        {
            switch (location)
            {
            case StorageStrategies.Local:
                return(await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(key, option));

            case StorageStrategies.Roaming:
                return(await Windows.Storage.ApplicationData.Current.RoamingFolder.CreateFileAsync(key, option));

            case StorageStrategies.Temporary:
                return(await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync(key, option));

            default:
                throw new NotSupportedException(location.ToString());
            }
        }
コード例 #11
0
        private async Task <StorageFile> CreateFileAsync(string key, StorageStrategies location = StorageStrategies.Local,
                                                         CreationCollisionOption option         = CreationCollisionOption.OpenIfExists, string path = null)
        {
            switch (location)
            {
            case StorageStrategies.Local:
                return(await ApplicationData.Current.LocalFolder.CreateFileAsync(key, option));

            case StorageStrategies.Roaming:
                return(await ApplicationData.Current.RoamingFolder.CreateFileAsync(key, option));

            case StorageStrategies.Temporary:
                return(await ApplicationData.Current.TemporaryFolder.CreateFileAsync(key, option));

            case StorageStrategies.Custom:
                //only using, if a filepicker is used. In other case, use CreateFileAsync(string key, StorageFolder folder,...)
                return(await StorageFile.GetFileFromPathAsync(path + key));

            default:
                throw new NotSupportedException(location.ToString());
            }
        }
コード例 #12
0
        /// <summary>Returns a file if it is found in the specified storage strategy</summary>
        /// <param name="key">Path of the file in storage</param>
        /// <param name="location">Location storage strategy</param>
        /// <param name="option">Specifies what to do if a file or folder with the specified name already exists in the current folder when you create a new file or folder</param>
        /// <returns>StorageFile</returns>
        private static async Task <StorageFile> GetIfFileExistsAsync(string key,
                                                                     StorageStrategies location     = StorageStrategies.Local,
                                                                     CreationCollisionOption option = CreationCollisionOption.FailIfExists)
        {
            StorageFile retval;

            try
            {
                switch (location)
                {
                case StorageStrategies.Local:
                    retval = await ApplicationData.Current.LocalFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.Roaming:
                    retval = await ApplicationData.Current.RoamingFolder.GetFileAsync(key);

                    break;

                case StorageStrategies.Temporary:
                    retval = await ApplicationData.Current.TemporaryFolder.GetFileAsync(key);

                    break;

                default:
                    throw new NotSupportedException(location.ToString());
                }
            }
            catch (FileNotFoundException)
            {
                Debug.WriteLine("** Error ** - GetIfFileExistsAsync:FileNotFoundException");
                return(null);
            }

            return(retval);
        }