示例#1
0
        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());
            }
        }
示例#2
0
        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());
            }
        }
示例#3
0
 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();
     }
 }
示例#4
0
        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;
        }
示例#5
0
        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);
        }