/// <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> /// Returns the encoding of the input file /// </summary> /// <param name="srcFile"></param> /// <returns></returns> public static Encoding GetFileEncoding(string srcFile) { Encoding encoding = Encoding.Default; if (string.IsNullOrEmpty(srcFile) || !File.Exists(srcFile)) { return(encoding); } // Read in the file in binary byte[] buffer; try { using (var file = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var memoryStream = new MemoryStream()) { file.CopyTo(memoryStream); buffer = memoryStream.ToArray(); } } } catch (Exception) { return(encoding); } // Detect encoding var textDetect = new TextEncodingDetect(); EncodingEnum textEnc = textDetect.DetectEncoding(buffer, buffer.Length); switch (textEnc) { case EncodingEnum.Ascii: // ASCII (chars in the 0-127 range) encoding = Encoding.ASCII; break; case EncodingEnum.Ansi: // ANSI (chars in the range 0-255 range) encoding = Encoding.GetEncoding(1252); break; case EncodingEnum.Utf8Bom: case EncodingEnum.Utf8Nobom: // UTF-8 encoding = Encoding.UTF8; break; case EncodingEnum.Utf16LeBom: case EncodingEnum.Utf16LeNobom: // UTF-16 Little Endian encoding = Encoding.Unicode; break; case EncodingEnum.Utf16BeBom: case EncodingEnum.Utf16BeNobom: // UTF-16 Big Endian encoding = Encoding.BigEndianUnicode; break; } return(encoding); }
/// <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> /// Returns the encoding of the input file /// </summary> /// <param name="srcFile"></param> /// <returns></returns> public static Encoding GetFileEncoding(string srcFile) { Encoding encoding = Encoding.Default; if (string.IsNullOrEmpty(srcFile) || !File.Exists(srcFile)) { return encoding; } // Read in the file in binary byte[] buffer; try { using (var file = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var memoryStream = new MemoryStream()) { file.CopyTo(memoryStream); buffer = memoryStream.ToArray(); } } } catch (Exception) { return encoding; } // Detect encoding var textDetect = new TextEncodingDetect(); EncodingEnum textEnc = textDetect.DetectEncoding(buffer, buffer.Length); switch (textEnc) { case EncodingEnum.Ascii: // ASCII (chars in the 0-127 range) encoding = Encoding.ASCII; break; case EncodingEnum.Ansi: // ANSI (chars in the range 0-255 range) encoding = Encoding.GetEncoding(1252); break; case EncodingEnum.Utf8Bom: case EncodingEnum.Utf8Nobom: // UTF-8 encoding = Encoding.UTF8; break; case EncodingEnum.Utf16LeBom: case EncodingEnum.Utf16LeNobom: // UTF-16 Little Endian encoding = Encoding.Unicode; break; case EncodingEnum.Utf16BeBom: case EncodingEnum.Utf16BeNobom: // UTF-16 Big Endian encoding = Encoding.BigEndianUnicode; break; } return encoding; }
/// <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!"); } }