//TODO: split out data functions from non data functions public static List <string> GetStringCollectionFromFile(string path) { var lines = new List <string>(); try { var reader = new StreamReader(path); string line; while ((line = reader.ReadLine()) != null) { lines.Add(line); } reader.Close(); } catch (FileNotFoundException ex) { AppService.LogLine(ex.Message); } catch (Exception ex) { AppService.LogLine(ex.Message); } return(lines); }
/// <summary> /// saves an object's properties to the specified path as key value pairs. overwrites existing file /// </summary> /// <param name="obj"></param> /// <param name="path"></param> public static void SaveObjectToFile(Object obj, string path) { var props = new List <KeyValuePair <string, string> >(); foreach (var prop in obj.GetType().GetProperties()) { props.Add(new KeyValuePair <string, string>(prop.Name, prop.GetValue(obj).ToString())); } EraseFile(path); try { var writer = new StreamWriter(path); foreach (KeyValuePair <string, string> prop in props) { writer.WriteLine(prop.Key + GlobalConstants.Delimiter + prop.Value); } writer.Close(); } catch (Exception e) { AppService.LogLine("Error saving: " + e.Message); } }
private void BtnConvert_Click(object sender, RoutedEventArgs e) { if (!string.IsNullOrWhiteSpace(txtConvertOld.Text) || !string.IsNullOrWhiteSpace(txtConvertNew.Text)) { var path = AppService.ShowOpenFileDialog(opts, "txt files (*.txt)|*.txt"); if (!string.IsNullOrWhiteSpace(path)) { var lines = FileIoService.GetStringCollectionFromFile(path); var newLines = new List <string>(); foreach (string line in lines) { newLines.Add(line.Replace(txtConvertOld.Text.Single(), txtConvertNew.Text.Single())); } FileIoService.SaveLineCollectionToFile(newLines, path); AppService.LogLine("Converted file: " + path); } else { MessageBox.Show("Path is required to convert file"); } } else { MessageBox.Show("Must have a value in both delimiter boxes"); } }
private void BtnLoad_Click(object sender, RoutedEventArgs e) { var fileName = AppService.ShowOpenFileDialog(opts, "txt files (*.txt)|*.txt"); if (!string.IsNullOrWhiteSpace(fileName)) { if (fileName.Split('.').Last().ToUpper().Equals("TXT")) { var itemObject = GetCurrentItemType(); itemObject = FileIoService.LoadObjectFromFile(itemObject, fileName, false); if (itemObject.IsNothing()) { LogLine("Item type mismatch. Select correct item type from drop down or add a new item via Options"); } else { LoadItem(itemObject, fileName); LogLine($"Loaded \"{fileName}\" as {itemObject.GetType().ToString()}"); } } else { AppService.LogLine($"Items must be in a TXT file format"); } } }
/// <summary> /// overwrites contents of file empty for writing. creates file if doesn't exist /// </summary> /// <param name="path"></param> private static void EraseFile(string path) { try { System.IO.File.WriteAllText(path, ""); } catch (Exception e) { AppService.LogLine(e.Message); } }
public static Object LoadObjectFromFile(Object objType, string path, bool createIfNotFound) { if (createIfNotFound) { CreateFileIfDoesntExist(path); } var fileLineValues = new List <KeyValuePair <string, string> >(); try { var reader = new StreamReader(path); string line; while ((line = reader.ReadLine()) != null) { var splitLine = line.Split(GlobalConstants.Delimiter); fileLineValues.Add(new KeyValuePair <string, string>(splitLine[0], splitLine[1])); } reader.Close(); } catch (FileNotFoundException ex) { AppService.LogLine(ex.Message); } catch (Exception ex) { AppService.LogLine("Error loading object: " + ex.Message); } var obj = Activator.CreateInstance(objType.GetType()); foreach (var prop in obj.GetType().GetProperties()) { var option = fileLineValues.FirstOrDefault(x => x.Key == prop.Name); try { if (option.Key != null) { prop.SetValue(obj, Convert.ChangeType(option.Value, prop.PropertyType)); } } catch (Exception e) { AppService.LogLine($"Mapping error for property: {prop.Name}. Error: {e.Message}"); return(Activator.CreateInstance(objType.GetType())); } } return(obj); }
private void BtnSave_Click(object sender, RoutedEventArgs e) { opts.MaxShortDescriptionTextLength = Convert.ToInt32(txtShortDescMaxSize.Text); opts.BasicFieldsSameAsAdvanced = chkBasicAdvancedEqual.IsChecked.HasValue && chkBasicAdvancedEqual.IsChecked.Value; opts.DefaultTermsPathP1 = txtTermsPath.Text; opts.DefaultTermsPathP2 = txtTermsPath_Copy.Text; AppService.SaveProgramOptions(opts); AppService.LogLine("Saved options"); AppService.RefreshMainWindowOptions(); this.Close(); }
public static void SaveSingleLineToFile(string html, string path) { EraseFile(path); try { var writer = new StreamWriter(path); writer.WriteLine(html); writer.Close(); } catch (Exception e) { AppService.LogLine(e.Message); } }
/// <summary> /// creates a new blank file if doesn't exist, else do nothing /// </summary> /// <param name="path"></param> public static bool CreateFileIfDoesntExist(string path) { bool fileExisted = false; if (!File.Exists(path)) { File.WriteAllText(path, ""); AppService.LogLine($"File {path} not found. Creating new file"); } else { fileExisted = true; } return(fileExisted); }
public static void SaveLineCollectionToFile(List <string> lines, string path) { EraseFile(path); try { var writer = new StreamWriter(path); foreach (string line in lines) { writer.WriteLine(line); } writer.Close(); } catch (Exception e) { AppService.LogLine(e.Message); } }
private void BtnSave_Click(object sender, RoutedEventArgs e) { //TODO: add basic and advanced combination on option flag c.BasicCPU = txtBasicCPU.Text; c.BasicRAM = txtBasicRAM.Text; c.BasicHDD = txtBasicHDD.Text; c.BasicOS = txtBasicOS.Text; c.CPUType = txtCPUType.Text; c.RAMCapacity = txtRAMCapacity.Text; c.RAMType = txtRAMType.Text; c.HDDSize = txtHDDSize.Text; c.CPUSpeed = txtCPUSpeed.Text; c.CPUCores = txtCPUCores.Text; c.CPUThreads = txtCPUThreads.Text; c.HDDQuantity = txtHDDQuantity.Text; c.HDDSpeed = txtHDDSpeed.Text; c.GFX = txtGFX.Text; c.ScreenSize = txtScreenSize.Text; c.ScreenRes = txtScreenRes.Text; c.ScreenType = txtScreenType.Text; c.Battery = txtBattery.Text; c.OSVersion = txtOSVersion.Text; c.Webcam = txtWebcam.Text; c.OtherDrives = txtOtherDrives.Text; c.Nics = txtNics.Text; c.ItemTitle = txtItemTitle.Text; c.ItemLongDescP1 = txtItemLongDescP1.Text; c.ItemLongDescP2 = txtItemLongDescP2.Text; c.HDDInterface = txtHDDInterface.Text; if (string.IsNullOrWhiteSpace(path)) { path = AppService.ShowSaveDialog(opts, "txt", "computer"); } if (!string.IsNullOrWhiteSpace(path)) { FileIoService.SaveObjectToFile(c, path); AppService.RefreshItem(c, path); AppService.RefreshMainWindowOptions(); AppService.LogLine($"Saved item to \"{path}\""); this.Close(); } }
private void BtnSave_Click(object sender, RoutedEventArgs e) { part.ItemTitle = txtItemTitle.Text; part.ItemLongDescP1 = txtItemLongDescP1.Text; part.ItemLongDescP2 = txtItemLongDescP2.Text; if (string.IsNullOrWhiteSpace(path)) { path = AppService.ShowSaveDialog(opts, "txt", "part"); } else { FileIoService.SaveObjectToFile(part, path); AppService.RefreshItem(part, path); AppService.RefreshMainWindowOptions(); AppService.LogLine($"Saved item to \"{path}\""); this.Close(); } }
/// <summary> /// copy a user specified html file to the template path /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnAddTemplate_Click(object sender, RoutedEventArgs e) { var path = AppService.ShowOpenFileDialog(opts, "Html-Files(*.html)|*.html"); if (!string.IsNullOrWhiteSpace(path)) { if (path.Split('.').Last().ToUpper().Equals("HTML")) { File.Copy(path, GlobalConstants.TemplatesPath + "\\" + path.Split('\\').Last(), true); AppService.LogLine($"Added template file \"{path}\" to Templates"); AppService.RefreshMainWindowOptions(); } else { AppService.LogLine($"Did not add template file \"{path}\". File must be HTML type"); } } else { MessageBox.Show("Must specify a file"); } }