public List <Config> Import(string pathFile) { var lstConfig = new List <Config>(); using (var fr = new StreamReader(pathFile)) { string line = ""; int index = 0; try { while ((line = fr.ReadLine()) != null) { index++; string[] arrLine = line.Split(','); if (arrLine.Count() == 2) { var nameValidate = ConfigValidate.NameValidate(arrLine[0], -1, lstConfig); var keywordValidate = ConfigValidate.KeywordValidate(arrLine[1], -1, lstConfig); if (!nameValidate.IsError && !keywordValidate.IsError) { lstConfig.Add(new Config { Name = arrLine[0], Keyword = arrLine[1] }); } else { if (!String.IsNullOrEmpty(nameValidate.Message)) { throw new Exception(nameValidate.Message + " Your config at line: " + index + " -> Invalid Name value = \n\"" + arrLine[0] + "\""); } if (!String.IsNullOrEmpty(keywordValidate.Message)) { throw new Exception(keywordValidate.Message + " Your config at line: " + index + "-> Invalid Keyword value = \n\"" + arrLine[1] + "\""); } break; } } else { throw new Exception("This item wasn't correct format <Name>,<Keyword>"); } } } catch (Exception ex) { lstConfig = null; throw new Exception("The config file is invalid. Please check your config file again :D" + Environment.NewLine + ex.Message); } return(lstConfig); } }
private void btnSave_Click(object sender, EventArgs e) { try { var nameValidate = ConfigValidate.NameValidate(txtName.Text, index, configs); var keywordValidate = ConfigValidate.KeywordValidate(txtKeyword.Text, index, configs); if (nameValidate.IsError) { errorProvider.SetError(txtName, nameValidate.Message); txtName.Focus(); } else { errorProvider.SetError(txtName, null); } if (keywordValidate.IsError) { errorProvider.SetError(txtKeyword, keywordValidate.Message); txtKeyword.Focus(); } else { errorProvider.SetError(txtKeyword, null); } if (!nameValidate.IsError && !keywordValidate.IsError) { ConfigViewModel = new ConfigViewModel { Name = txtName.Text, Keyword = txtKeyword.Text }; DialogResult = DialogResult.OK; this.Close(); } else { return; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Oops", MessageBoxButtons.OK, MessageBoxIcon.Error); ConfigViewModel = null; DialogResult = DialogResult.Cancel; return; } }