Package for serializing and deserializing vCards
$ dotnet add package vCard.NetvCard.Net is a .NET library designed to create, parse, and manipulate vCard files. vCards are electronic business cards that are widely used for sharing contact information.
To use vCard.Net in your project, install it via NuGet:
dotnet add package vCard.Net
Alternatively, you can clone this repository and build the project locally:
git clone https://github.com/gachris/vCard.Net.git
cd vCard.Net
dotnet build
Here’s a basic example to get you started with using vCard.Net:
using System;
using System.IO;
using System.Linq;
using vCard.Net.CardComponents;
using vCard.Net.Serialization;
// Read vCard data from a file
var filePath = "path/to/vCard_v21.vcf";
var vCardData = File.ReadAllText(filePath);
// Create a stream from the vCard data
using var memoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(vCardData));
using var reader = new StreamReader(memoryStream);
// Deserialize the vCard
var vCard = (VCard)SimpleDeserializer.Default.Deserialize(reader).First();
// Access some properties
Console.WriteLine(vCard.FormattedName);
Console.WriteLine(vCard.Emails.FirstOrDefault()?.Value);
using System;
using System.Collections.Generic;
using System.IO;
using vCard.Net.CardComponents;
using vCard.Net.DataTypes;
using vCard.Net.Serialization;
// Create a vCard
var vCard = new VCard
{
Version = VCardVersion.vCard2_1,
FormattedName = "John Doe",
N = new StructuredName { GivenName = "John", FamilyName = "Doe" },
Emails = new List<Email>
{
new Email
{
Value = "john.doe@example.com",
PreferredOrder = 1,
Types = new List<string> { "INTERNET" }
}
}
};
// Serialize the vCard to a string
var serializer = new ComponentSerializer();
var vCardAsString = serializer.SerializeToString(vCard);
// Save the vCard to a file
var outputPath = "path/to/new_vcard.vcf";
File.WriteAllText(outputPath, vCardAsString);
// Output
Console.WriteLine("vCard saved to: " + outputPath);
Contributions are welcome! Please follow these steps if you wish to contribute:
This project is licensed under the MIT License. See the LICENSE file for more details.