From 4ab015eea8084efcf5344d27826c6462fdc659dd Mon Sep 17 00:00:00 2001 From: ema Date: Fri, 13 Dec 2024 22:12:14 +0800 Subject: [PATCH] Fix CSV viewer not support formats other than UTF8 This may have the disadvantage of slowing down the startup speed of large CSV files. --- .../QuickLook.Plugin.CsvViewer/CsvViewerPanel.xaml.cs | 11 ++++++++--- .../QuickLook.Plugin.CsvViewer.csproj | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/CsvViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/CsvViewerPanel.xaml.cs index 78119822..02372dbf 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/CsvViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/CsvViewerPanel.xaml.cs @@ -22,10 +22,12 @@ using System.Globalization; using System.IO; using System.Linq; +using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; +using UtfUnknown; namespace QuickLook.Plugin.CsvViewer; @@ -39,14 +41,17 @@ public CsvViewerPanel() InitializeComponent(); } - public List Rows { get; private set; } = new List(); + public List Rows { get; private set; } = []; public void LoadFile(string path) { const int limit = 10000; var binded = false; - using (var sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) + var encoding = CharsetDetector.DetectFromFile(path).Detected?.Encoding ?? + Encoding.Default; + + using (var sr = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), encoding)) { var conf = new CsvConfiguration(CultureInfo.CurrentUICulture) { MissingFieldFound = null, BadDataFound = null, DetectDelimiter = true }; @@ -58,7 +63,7 @@ public void LoadFile(string path) var row = parser.Record; if (row == null) break; - row = Concat(new[] { $"{i++ + 1}".PadLeft(6) }, row); + row = Concat([$"{i++ + 1}".PadLeft(6)], row); if (!binded) { diff --git a/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/QuickLook.Plugin.CsvViewer.csproj b/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/QuickLook.Plugin.CsvViewer.csproj index fbd412d5..88aba7af 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/QuickLook.Plugin.CsvViewer.csproj +++ b/QuickLook.Plugin/QuickLook.Plugin.CsvViewer/QuickLook.Plugin.CsvViewer.csproj @@ -52,7 +52,8 @@ - + +