Note
To make the code more readable, exception handling has been omitted from the
examples.
public sealed class AnsiFilter
Public NotInheritable Class AnsiFilter
public ref class AnsiFilter sealed
[<SealedAttribute>]
type AnsiFilter = class end
VCF files of the vCard 2.1 standard were previously saved by Outlook and Outlook Express in the ANSI encoding of the current Windows system, without always encoding the extended characters with quoted printable encoding. In principle, VCF files of the vCard 3.0 standard can also be ANSI-encoded if, for example, they were transmitted in an e-mail that specified the encoding in its content header. UTF-8 encoding has only been mandatory since vCard 4.0.
This class is a wrapper around the Load(String, Encoding) method that initially loads the file as UTF-8. If a decoding error occurs, the method examines the VCard objects for any CharSet (CHARSET) parameters, which provide an indication of the Encoding to be used. If a hint is found, the file is reloaded with the determined Encoding, otherwise with the Encoding specified in the constructor as a fallback.
(The CHARSET parameter exists only in the vCard 2.1 standard.)
using System.Diagnostics;
using FolkerKinzel.VCards;
using FolkerKinzel.VCards.Extensions;
namespace Examples;
public static class AnsiFilterExample
{
/// <summary>
/// The example loads several vCard 2.1 files which have different encodings and
/// shows their content in the text editor. The encoding is selected automatically.
/// </summary>
/// <param name="directoryPath">Path to the directory containing the example files.</param>
/// <remarks>
/// See the VCF files used in this example at
/// https://github.com/FolkerKinzel/VCards/tree/master/src/Examples/AnsiFilterExamples
/// </remarks>
public static void LoadVcfFilesWhichHaveDifferentAnsiEncodings(string directoryPath)
{
// If you have to read VCF files that might have ANSI encodings, use the AnsiFilter
// to read them automatically with the right encoding.
// Give the constructor as an argument the ANSI code page that is most likely. This
// will be the fallback code page if a VCF file couldn't be loaded as UTF-8 and didn't
// contain a CHARSET parameter. In our example we choose windows-1255 (Hebrew).
var ansiFilter = new AnsiFilter(1255);
string outFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".txt");
using (StreamWriter writer = File.AppendText(outFileName))
{
foreach (string vcfFileName in Directory
.EnumerateFiles(directoryPath)
.Where(x => StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(x), ".vcf")))
{
IReadOnlyList<VCard> vCards = Vcf.Load(vcfFileName, ansiFilter);
WriteToTextFile(vcfFileName, vCards, ansiFilter.UsedEncoding.WebName, writer);
}
}
ShowInTextEditorAndDelete(outFileName);
}
private static void WriteToTextFile(string vcfFileName,
IEnumerable<VCard> vCards,
string? encodingWebName,
TextWriter writer)
{
const string indent = " ";
writer.Write(Path.GetFileName(vcfFileName));
writer.WriteLine(':');
writer.Write(indent);
writer.WriteLine(vCards.FirstOrDefault()?.DisplayNames.FirstOrNull()?.Value);
writer.Write(indent);
writer.Write("Encoding: ");
writer.WriteLine(encodingWebName);
writer.WriteLine();
}
private static void ShowInTextEditorAndDelete(string outFileName)
{
Process.Start(new ProcessStartInfo { FileName = outFileName, UseShellExecute = true })?
.WaitForExit();
File.Delete(outFileName);
}
}
/*
Output:
German.vcf:
Sören Täve Nüßlebaum
Encoding: windows-1252
Greek.vcf:
Βαγγέλης
Encoding: windows-1253
Hebrew.vcf:
אפרים קישון
Encoding: windows-1255
Ukrainian.vcf:
Віталій Володимирович Кличко
Encoding: windows-1251
utf-8.vcf:
孔夫子
Encoding: utf-8
Please note that Hebrew.vcf and utf-8.vcf have been read properly without
any CHARSET parameter in the VCF files: UTF-8 is the default character set
and windows-1255 (Hebrew) had been set as the default fallback value in the
AnsiFilter constructor.
*/
AnsiFilter(Int32) | Initializes an instance of the AnsiFilter class with the number of the code page to use for the reading of VCF files that are not UTF-8 and that do not contain a hint in their CHARSET parameters which suggests the use of a different Encoding. |
AnsiFilter(String) | Initializes an instance of the AnsiFilter class with the WebName property of the Encoding object to use for reading VCF files that are not UTF-8 and that do not contain a hint in their CHARSET parameters which suggests the use of a different Encoding. |
FallbackEncoding | The Encoding object to use as fallback. |
UsedEncoding | The Encoding used to read the VCF file. |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetHashCode | Serves as the default hash function. (Inherited from Object) |
GetType | Gets the Type of the current instance. (Inherited from Object) |
ToString | Returns a string that represents the current object. (Inherited from Object) |