public static void Save(string path, SaveInfo info) { try { SaveInfo = info; string json = JsonConvert.SerializeObject(info); if (json == null) { throw new Exception("Empty save info..."); } if (!File.Exists(path)) { using (FileStream fileStream = File.Create(path)) { Byte[] byteInfo = new UTF8Encoding(true).GetBytes(json); fileStream.Write(byteInfo, 0, byteInfo.Length); } } else { File.WriteAllText(path, json); } } catch (Exception e) { MessageBox.Show("Can't save setting, " + e.ToString()); } }
void Initialize() { saveInfo = new SaveInfo(); string cityListJson = string.Empty; byte[] cityListFile = Properties.Resources.CityList; Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream resourceStream = new MemoryStream(cityListFile)) { using (StreamReader streamReader = new StreamReader(resourceStream)) { cityListJson = streamReader.ReadToEnd(); } } cityList = new List <City>(); countryNames = new List <string>(); try { JArray cityListArray = JArray.Parse(cityListJson); for (int i = 0; i < cityListArray.Count; ++i) { City city = cityListArray[i].ToObject <City>(); if (!countryNames.Contains(city.Country)) { countryNames.Add(city.Country); } cityList.Add(city); } string[] countryArray = countryNames.ToArray(); Array.Sort(countryArray); foreach (string name in countryArray) { cmbCountryName.Items.Add(name); } } catch (Exception ex) { MessageBox.Show("Error.." + ex.ToString()); } }
public void LoadSetting() { if (Setting.IsSaveFileExist()) { saveInfo = Setting.Load(Setting.SAVE_PATH); UpdateUI(saveInfo); Hide(); } else { MessageBox.Show("No save found.\nPlease enter a valid API Key,\nSelect Country Code and City Name", "Error.."); ShowDialog(); } }
void UpdateUI(SaveInfo info) { cmbCountryName.Text = info.SelectedCountryCode; lstCityName.Items.Clear(); foreach (City city in cityList) { if (city.Country != info.SelectedCountryCode) { continue; } lstCityName.Items.Add(city.Name); if (city.ID == info.SelectedCityID) { lstCityName.SelectedIndex = (lstCityName.Items.Count - 1); } } txtApiKey.Text = info.APIKEY; }
public static SaveInfo Load(string path) { bool isFileExist = File.Exists(path); try { if (!isFileExist) { throw new FileNotFoundException(); } string json = File.ReadAllText(path); JObject saveObject = JObject.Parse(json); SaveInfo = saveObject.ToObject <SaveInfo>(); } catch (Exception e) { MessageBox.Show("Can't load setting, " + e.ToString()); } return(SaveInfo); }