//method to Read data from the configuration file private SpecificationData ReadConfig() { try { SpecificationData fileData = new SpecificationData(); string configurationFile = null, assemblyLocation = null; //open configuration file assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location; //convert the active file path into directory name if (File.Exists(assemblyLocation)) { assemblyLocation = new FileInfo(assemblyLocation).Directory.ToString(); } //get parent directory of the current directory if (Directory.Exists(assemblyLocation)) { assemblyLocation = Directory.GetParent(assemblyLocation).ToString(); } configurationFile = assemblyLocation + @"\Spacing Configuration\Configuration.txt"; if (!(File.Exists(configurationFile))) { TaskDialog.Show("Error!", "Configuration file doesn't exist!"); return(fileData); } else { //read all the contents of the file into a string array string[] fileContents = File.ReadAllLines(configurationFile); for (int i = 0; i < fileContents.Count(); i++) { if (fileContents[i].Contains("SelectedFamily: ")) { fileData.selectedFamily = fileContents[i].Replace("SelectedFamily: ", "").Trim(); } else if (fileContents[i].Contains("Discipline: ")) { fileData.discipline = fileContents[i].Replace("Discipline: ", "").Trim(); } else if (fileContents[i].Contains("Spacing: ")) { fileData.minSpacing = (int)(1000 * double.Parse(fileContents[i].Replace("Spacing: ", "").Trim())); } else if (fileContents[i].Contains("Support Type: ")) { fileData.supportType = fileContents[i].Replace("Support Type: ", "").Trim(); if (fileData.supportType.Contains("Threaded_Rod_Support")) { type = -1; } else if (fileData.supportType.Contains("Angle_Support")) { type = 1; } } else if (fileContents[i].Contains("File Location: ")) { fileData.specsFile = fileContents[i].Replace("File Location: ", "").Trim(); } } } return(fileData); } catch { throw new Exception(); } }
//constructors public ConfigurationData() { configFileData = ReadConfig(); GetElectricalSpecs(); }