public void LoadAllVoteFilesFromDisk() { if (UseLocalFiles && VoteFilesDirectory != null) { VoteFiles.Clear(); foreach (FileInfo item in VoteFilesDirectory.GetFiles()) { if (item.Name.EndsWith(".json")) { VoteFile newVF = null; try { newVF = VoteFile.FromLoad(item.FullName, this); } catch (Exception e) { App.Log(e.Message, this); } if (newVF != null) { if (newVF.Validate(this)) { VoteFiles.Add(newVF); } } } } VoteFilesLoaded = true; LastVoteFileLoadTime = DateTime.Now; } }
private static VoteFile LoadOrCreate(string path, ServerSettings settings) { //if file exists - load if (File.Exists(path)) { try { string json = File.ReadAllText(path); VoteFile newVF = JsonConvert.DeserializeObject <VoteFile>(json); if (!newVF.IsValid) { try { newVF = JsonConvert.DeserializeObject <VoteFile>(BackupVotingJson1); } catch (Exception e) { App.Log("Failed to serialize default vote file from backup json | " + path + " | " + e.Message, settings); } newVF.Validate(settings); if (newVF != null && newVF.IsValid) { newVF.Path = path; return(newVF); } else { return(null); } } else { return(newVF); } } catch (Exception e) { App.Log("Failed to load vote file \"" + path + "\" | " + e.Message, settings); return(null); } } //if not, create else { if (Directory.Exists(System.IO.Path.GetDirectoryName(path))) { VoteFile newVF = new VoteFile() { Path = path }; newVF.WriteToDisk(); return(newVF); } else { return(null); } } }