public ConfigObject GetConfig() { string data = System.IO.File.ReadAllText("config.json"); ConfigObject config = JsonConvert.DeserializeObject <ConfigObject>(data); return(config); }
/// <summary> /// This void will prepare and initialize all sorts of things. It has to be run first /// </summary> public void Initialize() { Console.WriteLine("[//] Initializing ConstantWrite torture test [\\\\]"); Console.WriteLine("[//] Author: " + author + " [\\\\]"); Console.WriteLine("[//] Version: " + version + " [\\\\]"); Console.WriteLine("[//] GitHub: " + github + " [\\\\]"); Console.WriteLine("[//] License: " + license + " [\\\\]"); // Read config Console.Write("[#] Reading config.json"); config = ConfigClass.GetConfig(); ClearCurrentConsoleLine(); Console.WriteLine("[+] Loaded config.json"); // Validate config Console.Write("[#] Validating settings"); bool valid = true; if (config.FileSize < 1) { valid = false; } ; if (!config.StoragePath.EndsWith("/") || !config.StoragePath.EndsWith("\\")) { config.StoragePath = config.StoragePath += "/"; } try { if (!Directory.Exists(config.StoragePath)) { Directory.CreateDirectory(config.StoragePath); } } catch (Exception ex) { valid = false; ClearCurrentConsoleLine(); Console.WriteLine("[+] Error: " + ex.ToString()); } ClearCurrentConsoleLine(); if (valid == true) { Console.WriteLine("[+] Validation complete"); } else { Console.WriteLine("[!] Validation failed"); } Console.WriteLine("[//] Initialization finnished [\\\\]" + Environment.NewLine); }
public void OpenConfigEditor() { // CLI editor for config Console.Write("Please Wait (...)"); ConfigObject config = GetConfig(); ClearCurrentConsoleLine(); while (AllowExit == false) { Console.WriteLine("[----- Configuration -----]"); Console.WriteLine("1. Drive/Folder to torture test"); Console.WriteLine("2. Size of test files"); Console.WriteLine("3. Autoclean after end of test"); Console.WriteLine("4. Return to main menu"); Console.WriteLine(); string menu_pool = "1234"; string menu_selection = "#"; // ask what to start while (!menu_pool.Contains(menu_selection)) { ClearCurrentConsoleLine(); Console.Write("What do you want to configure? Number: "); menu_selection = Console.ReadKey().KeyChar.ToString(); } Console.WriteLine(System.Environment.NewLine); // for nice formatting int id = Convert.ToInt32(menu_selection); switch (id) { case 1: Console.WriteLine("Editing torture test location (leave blank for current)"); Console.WriteLine("Current: " + config.StoragePath); Console.Write("New: "); string newdata1 = Console.ReadLine(); if (newdata1 == "" || newdata1 == " ") { Console.WriteLine("Canceled!"); } else { Console.Write("Saving ..."); config.StoragePath = newdata1; SaveConfig(config); ClearCurrentConsoleLine(); Console.WriteLine("Changes saved!"); } Console.WriteLine(); // for nice formatting break; case 2: Console.WriteLine("Editing test file size (leave blank for current)"); Console.WriteLine("INFO: This number will be the size of test files in MB"); Console.WriteLine("Current: " + config.FileSize); Console.Write("New: "); string newdata2 = Console.ReadLine(); if (newdata2 == "" || newdata2 == " ") { Console.WriteLine("Canceled!"); } else { if (Convert.ToInt32(newdata2) < 1) { newdata2 = "1"; Console.WriteLine("INFO: Your input was autocorrected to 1 MB because the size cannot be samller than 1 MB"); } Console.Write("Saving ..."); config.FileSize = Convert.ToInt32(newdata2); SaveConfig(config); ClearCurrentConsoleLine(); Console.WriteLine("Changes saved!"); } Console.WriteLine(); // for nice formatting break; case 3: Console.WriteLine("Editing automatic cleanup of test files (leave blank for current)"); Console.WriteLine("INFO: This setting can only be 'true' or 'false'"); Console.WriteLine("Current: " + config.AutoClean); Console.Write("New: "); string newdata3 = Console.ReadLine(); if (newdata3 == "" || newdata3 == " ") { Console.WriteLine("Canceled!"); } else { if (newdata3 == "true" || newdata3 == "True") { Console.Write("Saving ..."); config.AutoClean = true; SaveConfig(config); ClearCurrentConsoleLine(); Console.WriteLine("Changes saved!"); } else if (newdata3 == "false" || newdata3 == "False") { Console.Write("Saving ..."); config.AutoClean = false; SaveConfig(config); ClearCurrentConsoleLine(); Console.WriteLine("Changes saved!"); } else if (newdata3 != "true" && newdata3 != "false") { Console.WriteLine("Error: Invalid input"); } } Console.WriteLine(); // for nice formatting break; case 4: // end menu loop to fall back to main menu AllowExit = true; break; } } }
private void SaveConfig(ConfigObject config) { string data = JsonConvert.SerializeObject(config, Formatting.Indented); System.IO.File.WriteAllText("config.json", data); }