string SelectedOption = string.Empty; //переменная для хранения выбранной опции private void frmVPNOptions_Load(object sender, EventArgs e) { VPNOptions = Settings.GetVPNOptions(); //добавляем существующие элементы в ListBox string[] tmpitems = new string[VPNOptions.Keys.Count]; VPNOptions.Keys.CopyTo(tmpitems, 0); lstOptions.Items.AddRange(tmpitems); }
private bool AddOptions(string FileName) { ErrorMessage = ""; Dictionary <string, string> VPNOptions = Settings.GetVPNOptions(); if (VPNOptions.Count == 0) { return(true); } string[] ConfigBuf = null; try { ConfigBuf = File.ReadAllLines(FileName); } catch (Exception ex) { ErrorMessage = ex.Message; return(false); } List <string> AddedOptions = new List <string>(); foreach (string key in VPNOptions.Keys) { int n = GetNumberConfigString(ConfigBuf, key); if (n == -1) //строки в конфиге еще нет, добавляем в отдельный List { AddedOptions.Add(key + " " + VPNOptions[key]); } else //строка есть, меняем { ConfigBuf[n] = key + " " + VPNOptions[key]; } } //сохраняем измененный файл StreamWriter sw = null; try { sw = new StreamWriter(FileName); foreach (string s in AddedOptions) { sw.WriteLine(s); } foreach (string s in ConfigBuf) { sw.WriteLine(s); } } catch (Exception ex) { if (sw != null) { sw.Close(); } ErrorMessage = ex.Message; return(false); } return(true); }