/// <summary> /// Read all the text of a file in one go, same as File.ReadAllText expect it's truly a read only function /// </summary> public static string ReadAllText(string path, Encoding encoding = null) { using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var textReader = new StreamReader(fileStream, encoding ?? TextEncodingDetect.GetFileEncoding(path))) { return(textReader.ReadToEnd()); } }
/// <summary> /// Read all the text of a file in one go, same as File.ReadAllText expect it's truly a read only function /// </summary> public static string ReadAllText(string path, Encoding encoding = null) { try { using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var textReader = new StreamReader(fileStream, encoding ?? TextEncodingDetect.GetFileEncoding(path))) { return(textReader.ReadToEnd()); } } } catch (Exception e) { ErrorHandler.ShowErrors(e, "Error while reading the following file : " + path); } return(null); }
/// <summary> /// Called on an update, allows to do special stuff according to the version updated /// </summary> private void UpdateDoneFromVersion(string fromVersion) { // reset the log files Utils.DeleteDirectory(Config.FolderLog, true); if (!fromVersion.IsHigherVersionThan("1.7.3")) { Utils.DeleteDirectory(Path.Combine(Npp.ConfigDirectory, "Libraries"), true); } if (!fromVersion.IsHigherVersionThan("1.7.6")) { // delete old UDL if (File.Exists(Npp.ConfXml.FileNppUserDefinedLang)) { var encoding = TextEncodingDetect.GetFileEncoding(Npp.ConfXml.FileNppUserDefinedLang); var fileContent = Utils.ReadAllText(Npp.ConfXml.FileNppUserDefinedLang, encoding); var regex = new Regex("<UserLang name=\"OpenEdgeABL\".*?</UserLang>", RegexOptions.Singleline | RegexOptions.IgnoreCase); var matches = regex.Match(fileContent); if (matches.Success) { fileContent = regex.Replace(fileContent, ""); // write to userDefinedLang.xml var copyPath = Path.Combine(Config.FolderUpdate, "userDefineLang.xml"); Utils.FileWriteAllText(copyPath, fileContent, encoding); // replace default file by its copy on npp shutdown _3PUpdater.Instance.AddFileToMove(copyPath, Npp.ConfXml.FileNppUserDefinedLang); } } } if (!fromVersion.IsHigherVersionThan("1.7.8")) { // delete old database dump try { if (Directory.Exists(Config.FolderDatabase)) { foreach (string file in Directory.EnumerateFiles(Config.FolderDatabase, "*.dump", SearchOption.TopDirectoryOnly)) { File.Delete(file); } } } catch (Exception e) { ErrorHandler.LogError(e); } } }
/// <summary> /// Reads all the line of either the filePath (if the file exists) or from byte array dataResources, /// Apply the action toApplyOnEachLine(int lineNumber, string lineString) to each line /// Uses encoding as the Encoding to read the file or convert the byte array to a string /// Uses the char # as a comment in the file (must be the first char of a line) /// Returns the line w/o the \r\n /// </summary> public static void ForEachLine(string filePath, byte[] dataResources, Action <int, string> toApplyOnEachLine, Encoding encoding = null) { try { Exception ex = new Exception("Undetermined"); if (!Utilities.ForEachLine(filePath, dataResources, toApplyOnEachLine, encoding ?? TextEncodingDetect.GetFileEncoding(filePath), exception => ex = exception)) { ErrorHandler.ShowErrors(ex, "Error reading file", filePath); } } catch (Exception e) { ErrorHandler.ShowErrors(e, "Error while reading an internal ressource!"); } }