Exemplo n.º 1
0
        private void LoadJSONFile()
        {
            //if (File.Exists(DbFilePath))
            if (fileStore.Exists(DbFilePath))
            {
                string content = null;
                if (fileStore.TryReadTextFile(DbFilePath, out content))
                {
                    var settings = new JsonSerializerSettings()
                    {
                        TypeNameHandling     = TypeNameHandling.Objects,
                        DefaultValueHandling = DefaultValueHandling.Populate
                    };

                    this.localDb = JsonConvert.DeserializeObject <IDictionary <string, T> >(content, settings);

                    if (this.localDb == null)
                    {
                        this.localDb = new ConcurrentDictionary <string, T>(); // Empty file
                    }
                }
                else
                {
                    throw new IOException(string.Format("Cannot read json repository file '{0}'", DbFilePath));
                }
            }
            else
            {
                this.localDb = new Dictionary <string, T>();
            }
            LoadDbConfigFile();
        }
Exemplo n.º 2
0
        private async Task loadEntries()
        {
            if (storage.Exists(_indexFileName))
            {
                // Load It

                string contents;
                if (storage.TryReadTextFile(_indexFileName, out contents))
                {
                    var list = JsonConvert.DeserializeObject <List <Entry> >(contents);
                    _indexByHttp = list.ToDictionary(x => x.HttpSource, x => x);
                }

                //try
                //{
                //    contents = await asyncStorage.TryReadTextFile(_indexFileName);
                //    var list = JsonConvert.DeserializeObject<List<Entry>>(contents);
                //    _indexByHttp = list.ToDictionary(x => x.HttpSource, x => x);

                //}
                //catch (Exception e)
                //{
                //    Mvx.Trace("Could not open file inside LoadEntries - CacheService : " + e.Message);
                //}
            }
        }
Exemplo n.º 3
0
        public PropertyFinderPersistentState LoadState()
        {
            PropertyFinderPersistentState state = null;

            try
            {
                string data;
                if (_fileStore.TryReadTextFile(FileName, out data))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(PropertyFinderPersistentState));
                    TextReader    reader     = new StringReader(data);
                    state = (PropertyFinderPersistentState)serializer.Deserialize(reader);
                    state.PersistenceService = this;
                }
            }
            catch
            {
            }

            if (state == null)
            {
                state = new PropertyFinderPersistentState(this);
            }
            return(state);
        }
Exemplo n.º 4
0
        private void DeleteOldEntries(string fileName, int overflow)
        {
            var backupFileName = fileName + ".bak";

            try
            {
                if (_fileStoreService.Exists(backupFileName))
                {
                    _fileStoreService.DeleteFile(backupFileName);
                }

                // Creating a backup copy of the log in case a crash happends.
                _fileStoreService.TryMove(fileName, backupFileName, false);

                // read the content of file and remove old entries
                string fileContentString = string.Empty;
                _fileStoreService.TryReadTextFile(fileName, out fileContentString);
                var logContent = RemoveOlderEntries(fileContentString, overflow);

                // delete current log file en rewrite it without old entries
                _fileStoreService.DeleteFile(fileName);
                _fileStoreService.WriteFile(fileName, x => GenerateStreamFromString(logContent));

                // deleting the backup copy
                _fileStoreService.DeleteFile(backupFileName);
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the data from the file system
        /// </summary>
        /// <returns>An instance of the saved data, or a new instance if none had been saved</returns>
        public FeedbackData GetData()
        {
            string feedbackDataBuffer;

            return(_fileStore.TryReadTextFile(DataFileName, out feedbackDataBuffer) ?
                   _jsonConverter.DeserializeObject <FeedbackData>(feedbackDataBuffer) :
                   new FeedbackData());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Load data from file
        /// </summary>
        /// <typeparam name="T">Type of data</typeparam>
        /// <param name="fileName">File's name</param>
        /// <returns>Data from file</returns>
        public T Load <T>(string fileName)
        {
            string txt;
            T      result = default(T);

            if (_fileStore.TryReadTextFile(fileName, out txt))
            {
                return(_jsonConverter.Deserialize <T>(txt));
            }
            return(result);
        }
Exemplo n.º 7
0
        public void Init(CommunicationParameters parameters)
        {
            //Ler o arquivo
            string contents;

            _fileStore.TryReadTextFile(Constants.FileName, out contents);
            Value = int.Parse(contents);

            //Utilizar os parametros
            Value = parameters.SomeInteger;
        }
Exemplo n.º 8
0
        private void Load()
        {
            string fileContent;

            if (m_FileStore.TryReadTextFile(Path.Combine(m_PlatformInfo.BaseDirectory, m_FileName), out fileContent))
            {
                m_Data = JsonConvert.DeserializeObject <Dictionary <string, string> >(fileContent);
            }
            else
            {
                m_Data = new Dictionary <string, string>();
            }
        }
 //Van file lezen
 public static void ReadFromFile()
 {
     try
     {
         string contents = string.Empty;
         _fileStore.TryReadTextFile(_folderName + "/" + _fileName, out contents);
         GlobalVariables.FileValue = contents;
         //return Tekst = contents;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 10
0
        private void FillLibrary()
        {
            if (_myLibrary == null)
            {
                string contents = string.Empty;
                _fileStore.TryReadTextFile("MyBookApp/Library", out contents);

                if (contents != null)
                {
                    _myLibrary = new List <Book>();
                    _myLibrary = JsonConvert.DeserializeObject <List <Book> >(contents);
                }
            }
        }
Exemplo n.º 11
0
        private string ReadFromFile(string _fileName)
        {
            string _folderName = "NewJob";

            try
            {
                string contents = string.Empty;
                _fileStore.TryReadTextFile(_folderName + "/" + _fileName, out contents);
                return(contents);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 12
0
        public FavoritesService(IMvxFileStore fileStore, IMvxMessenger messenger)
        {
            _fileStore = fileStore;
            _messenger = messenger;

            string contents;

            if (_fileStore.TryReadTextFile(FavoritesFile, out contents))
            {
                Load(contents);
            }
            else
            {
                Save();
            }
        }
        public IEnumerable<PetViewModel> GetPets()
        {
            var json = string.Empty;

            if (_fileStore.TryReadTextFile(_storedFile, out json))
            {
                return JsonConvert.DeserializeObject<IEnumerable<PetViewModel>>(json);
            }

            return new[]
            {
                new PetViewModel { Name = "Pet 1" },
                new PetViewModel { Name = "Pet 2" },
                new PetViewModel { Name = "Pet 3" },
                new PetViewModel { Name = "Pet 4" },
                new PetViewModel { Name = "Pet 5" }
            };
        }
Exemplo n.º 14
0
        public Task <bool> SilentLoginAsync()
        {
            return(Task.Run <bool>(delegate
            {
                if (!fileStore.Exists(TokenPath))
                {
                    return false;
                }

                string fileText;
                if (!fileStore.TryReadTextFile(TokenPath, out fileText))
                {
                    return false;
                }

                token = JsonConvert.DeserializeObject <OAuthToken>(fileText);

                return true;
            }));
        }
        private async Task LoadDataService(IDataService dataService, bool overrideCache = false)
        {
            List <Item> currentItems = new List <Item>();

            bool loadedFromCache = false;

            if (fileStore != null)
            {
                string lastRefreshText;
                if (fileStore.TryReadTextFile("LastRefresh-" + dataService.GetType().ToString(), out lastRefreshText))
                {
                    var lastRefreshTime = DateTime.Parse(lastRefreshText);
                    var timeSinceLastRefreshInMinutes = (DateTime.Now - lastRefreshTime).TotalMinutes;

                    //has cache expired?
                    if (overrideCache || timeSinceLastRefreshInMinutes > AppSettings.CacheIntervalInMinutes)
                    {
                        currentItems = await dataService.GetItems();
                    }
                    else //load from cache
                    {
                        string cachedItemsText;
                        pool.WaitOne();
                        try
                        {
                            if (fileStore.TryReadTextFile("CachedItems-" + dataService.GetType().ToString(), out cachedItemsText))
                            {
                                currentItems    = mvxJsonConverter.DeserializeObject <List <Item> >(cachedItemsText);
                                loadedFromCache = true;
                            }
                        }
                        catch
                        {
                            ServiceLocator.MessageService.ShowErrorAsync("Error Deserializing " + dataService.GetType().ToString(), "Error loading cache");
                        }
                        finally
                        {
                            pool.Release();
                        }
                    }
                }
                else
                {
                    currentItems = await dataService.GetItems();
                }
            }

            try
            {
                if (!loadedFromCache && currentItems.Count > 0)
                {
                    if (fileStore.Exists("CachedItems-" + dataService.GetType().ToString()))
                    {
                        fileStore.DeleteFile("CachedItems-" + dataService.GetType().ToString());
                    }

                    if (fileStore.Exists("LastRefresh-" + dataService.GetType().ToString()))
                    {
                        fileStore.DeleteFile("LastRefresh-" + dataService.GetType().ToString());
                    }

                    fileStore.WriteFile("CachedItems-" + dataService.GetType().ToString(), mvxJsonConverter.SerializeObject(currentItems));
                    fileStore.WriteFile("LastRefresh-" + dataService.GetType().ToString(), DateTime.Now.ToString());
                }

                items.AddRange(currentItems);
            }
            catch
            {
                ServiceLocator.MessageService.ShowErrorAsync("Error retrieving items from " + dataService.GetType().ToString() + "\n\nPossible Causes:\nNo internet connection\nRemote Service unavailable", "Application Error");
            }
        }
Exemplo n.º 16
0
        public async void GetMorningJuice()
        {
            try
            {
                Recipes = await _recipeService.GetRecipes();

                int counter = Recipes.Count;

                Random   rnd          = new Random();
                int      RandomNumber = rnd.Next(1, counter);
                string   rndNumToStr  = RandomNumber.ToString();
                DateTime dateAndTime  = DateTime.Now;
                string   day          = dateAndTime.ToString("dd/MM/yyyy");
                string   folderValue  = (day + "," + rndNumToStr);
                var      _folderName  = "TextFilesFolder1";
                var      _fileName    = "MorningJuice";

                if (!_fileStore.FolderExists(_folderName))
                {
                    _fileStore.EnsureFolderExists(_folderName);
                }

                //Content van de file uitlezen
                string value = string.Empty;
                _fileStore.TryReadTextFile(_folderName + "/" + _fileName, out (value));
                string   CheckFileContent = value;
                string[] TextFileList;

                //Als er niets in zit, default data in steken
                if (CheckFileContent == null)
                {
                    _fileStore.WriteFile(_folderName + "/" + _fileName, "00/00/00,0");
                    string d = "00/00/00,0";
                    TextFileList = d.Split(',');
                }
                else
                {
                    TextFileList = CheckFileContent.Split(',');
                }

                if (TextFileList[0] != day)
                {
                    try
                    {
                        //File verwijderen om overbodige data te verwijderen.
                        _fileStore.DeleteFile(_folderName + "/" + _fileName);
                        //File aanmaken.
                        if (!_fileStore.FolderExists(_folderName))
                        {
                            _fileStore.EnsureFolderExists(_folderName);
                        }

                        _fileStore.WriteFile(_folderName + "/" + _fileName, folderValue);
                        string NewValue = string.Empty;
                        _fileStore.TryReadTextFile(_folderName + "/" + _fileName, out (NewValue));
                        string NValue = NewValue;

                        List <string> NewTextFileList = new List <string>(
                            NValue.Split(new string[] { "," }, StringSplitOptions.None));

                        int numVall        = Int32.Parse(NewTextFileList[1]);
                        int NewRandomValue = numVall;
                        MorningContent = await _recipeService.GetRecipeById(NewRandomValue);

                        RaisePropertyChanged(() => MorningContent);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    int numVall        = Int32.Parse(TextFileList[1]);
                    int NewRandomValue = numVall;
                    MorningContent = await _recipeService.GetRecipeById(NewRandomValue);

                    RaisePropertyChanged(() => MorningContent);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }