Files
PowerToys/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/Helpers/ReadHelper.cs
gokcekantarci 89a87e6db4 [Peek]Peek and Monaco file encoding issues are solved with a encoding detector (#26955)
* [Peek] Peek and Monaco file encoding issues are solved with a encoding detector.

* [Peek] Monaco encoding parameter is moved to another function.

* [Peek] NOTICE.md update.

* [Peek] Spell Check update.

* UTF-Unknown is added to Nuget list in Notice.md

* System.Text.Encoding.CodePages is added to Nuget list in Notice.md

* [Peek] Unncessary mainfest files are deleted.

* [Peek ] Unncessary mainfest file are deleted.

* [Peek] Encoding null check is added.

* Update NOTICE.md

* Update NOTICE.md

* ci: Add signing to UtfUnknown
2023-11-14 15:27:45 +00:00

35 lines
1.1 KiB
C#

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.IO;
using System.Text;
using System.Threading.Tasks;
using UtfUnknown;
namespace Peek.FilePreviewer.Previewers
{
public static class ReadHelper
{
public static async Task<string> Read(string path)
{
DetectionResult result = CharsetDetector.DetectFromFile(path);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Check if the detected encoding is not null, otherwise default to UTF-8
Encoding encodingToUse = result.Detected?.Encoding ?? Encoding.UTF8;
using var fs = OpenReadOnly(path);
using var sr = new StreamReader(fs, encodingToUse);
string content = await sr.ReadToEndAsync();
return content;
}
public static FileStream OpenReadOnly(string path)
{
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
}
}
}