我目前正在开发一个 UWP 项目,想要创建一个 ANSI 字符集(Windows-1252、ISO-8859-1)的文本文件。
此文件的阅读器仅支持 ANSI。如果读取端现在期望 ANSI 格式并接收 UTF8 编码中的特殊字符,则这些从 160 到 255 的 ANSI 字符将被错误解释。
在 UWP 世界中你需要这个:
StorageFile file = await DownloadsFolder.CreateFileAsync("Test.txt");await Windows.Storage.FileIO.WriteTextAsync(file, "Hällö", Windows.Storage.Streams.UnicodeEncoding.Utf8);
不幸的是,这里只能使用Unicode编码,不能使用ANSI!
另一种选择是
using (TextWriter tw = new StreamWriter(p, true, Encoding.GetEncoding("ISO-8859-1")))
但是,在 UWP 沙箱之外不允许这样做。
那么,有没有办法从使用 ANSI 字符集(Windows-1252、ISO-8859-1)的 UWP 应用程序创建简单的文本文件?
有人有想法吗?
问候托斯顿
要创建代码页 1252 (Windows-1252) 的编码,请添加 nuget 包 System.Text.Encoding.CodePages:
using System.Text;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding encoding = Encoding.GetEncoding(1252); // Western European (Windows)