/// <summary> /// Function used to save the variables into an external file. /// </summary> public static void saveVariablesIntoExternalFile() { Hashtable vars = VariableFileHandle.getVariables(); removeIrrelevantVariables(vars); //if the variable table is empty then throw an error. if (vars.Count == 0) { EmptyVarSaveException err = new EmptyVarSaveException("Variables Table is Empty."); ErrorMsg eMsg = new ErrorMsg(err.Message, err.ErrorCode); eMsg.ShowDialog(); return; } //Save the variables SaveFileDialog saveVar = new SaveFileDialog(); saveVar.Filter = "JSON file(*.json)|*.json"; saveVar.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (saveVar.ShowDialog() == true) { TextWriter TW = new StreamWriter(saveVar.FileName); int counter = 0; int total = vars.Count; TW.WriteLine("["); foreach (DictionaryEntry pair in vars) { string key = (string)pair.Key; string value = (string)pair.Value; TW.WriteLine("\t{"); TW.WriteLine("\t\t\"name\": " + "\"" + key + "\","); TW.WriteLine("\t\t\"value\": " + "\"" + value + "\""); if (counter == (total - 1)) { TW.WriteLine("\t}"); } else { TW.WriteLine("\t},"); } counter++; } TW.WriteLine("]"); TW.Close(); } }
/// <summary> /// Function used by the saveAll function. /// </summary> /// <param name="location"></param> public static void saveVariablesIntoExternalFile(string location) { Console.WriteLine(location); Hashtable vars = VariableFileHandle.getVariables(); removeIrrelevantVariables(vars); //if the variable table is empty then throw an error. if (vars.Count == 0) { EmptyVarSaveException err = new EmptyVarSaveException("Variables Table is Empty."); ErrorMsg eMsg = new ErrorMsg(err.Message, err.ErrorCode); eMsg.ShowDialog(); return; } //Save the variables TextWriter TW = new StreamWriter(Path.Combine(location)); int counter = 0; int total = vars.Count; TW.WriteLine("["); foreach (DictionaryEntry pair in vars) { string key = (string)pair.Key; string value = (string)pair.Value; TW.WriteLine("\t{"); TW.WriteLine("\t\t\"name\": " + "\"" + key + "\","); TW.WriteLine("\t\t\"value\": " + "\"" + value + "\""); if (counter == (total - 1)) { TW.WriteLine("\t}"); } else { TW.WriteLine("\t},"); } counter++; } TW.WriteLine("]"); TW.Close(); }