/// <summary>Overrite a file text with new text</summary> /// <param name="toWrite">New file text</param> public static bool OverriteFile(string fileName, string[] toWrite, bool ifEmpty = false) { File_Inst loadedFile = GetLoadedFile(fileName); if (loadedFile == null) { return(false); //Ensure file exists and is loaded } if (ifEmpty) { if (!loadedFile.IsEmpty()) { return(false); //Only overrite if file is empty } } bool written = WriteToFile(loadedFile, toWrite); if (written) //Inform whether file was written successfully { return(true); } else { return(false); } }
/// <summary>Edit lines of text based on the given search terms</summary> public static bool EditFile(string fileName, string[] terms, string[] text) { ValidateArray(terms, text); //Ensure lines and text have the same length File_Inst loadedFile = GetLoadedFile(fileName); if (loadedFile == null) { return(false); //Ensure file exists and is loaded } string[] fileContents = ReadFromFile(loadedFile); for (int n = 0; n < terms.Length; n++) { if (fileContents[n][0] != '#') //Ignore comment lines { string splitString = fileContents[n].Split('=')[0]; //Split the string around the '=' symbol giving the setting term if (splitString == terms[n]) { fileContents[n] = terms[n] + "=" + text; } } } bool written = WriteToFile(loadedFile, fileContents); if (written) //Inform whether file was written successfully { return(true); } else { return(false); } }
/// <summary>Edit specific lines of text in the file</summary> public static bool EditFile(string fileName, int[] lines, string[] text) { ValidateArray(lines, text); //Ensure lines and text have the same length File_Inst loadedFile = GetLoadedFile(fileName); if (loadedFile == null) { return(false); //Ensure file exists and is loaded } string[] fileContents = ReadFromFile(loadedFile); for (int n = 0; n < lines.Length; n++) { fileContents[lines[n]] = text[n]; } bool written = WriteToFile(loadedFile, fileContents); if (written) //Inform whether file was written successfully { return(true); } else { return(false); } }
/// <summary> /// Returns a single line from a file /// </summary> /// <param name="fileName">Name of file loaded</param> /// <param name="line">Line requested</param> /// <returns></returns> public static string ReadLine(string fileName, int line) { File_Inst loadedFile = GetLoadedFile(fileName); if (loadedFile == null) { return(null); //Ensure file exists and is loaded } string[] allText = ReadFromFile(loadedFile); return(allText[line]); }
private static string fileEncryptionPass = "******"; //Note it is advised that this is not 'hardcoded' and is instead stored securely (possibly linked to a login) #region File Commands /// <summary>Load a file into the file handler, note will create file if does not exist</summary> /// <param name="fileToAdd">The file to be added</param> public static void LoadFile(File_Inst fileToAdd) { if (!IsDuplicate(fileToAdd.Name)) { if (!File.Exists(fileToAdd.Path)) { //Create file if does not exist File.Create(fileToAdd.Path + "\\" + fileToAdd.Name).Dispose(); } OnHandFiles.Add(fileToAdd); //Add file to handler memory } }
/// <summary> /// Returns a single line from a file matching the search criteria /// </summary> /// <param name="fileName">Name of file loaded</param> /// <param name="searchTerm">Search criteria</param> /// <returns></returns> public static string ReadLine(string fileName, string searchTerm) { File_Inst loadedFile = GetLoadedFile(fileName); if (loadedFile == null) { return(null); //Ensure file exists and is loaded } string[] allText = ReadFromFile(loadedFile); foreach (string s in allText) { if (s.Contains(searchTerm)) { return(s); } } return(null); }