What type of file does your phone saves when u create a contact?
Well, the answer is: It stores it in a file format called vcard. Where vcard stands for "Virtual Contact Card".
At the time of writing this article., there are not a lot of nuget packages that can do this.
So, in this tutorial, we are going to create a vcard using C#.
Create a vcard using C#
Let's get started by creating a project on visual studio.
Click on new project and select "console application".
Click on continue and give it a name.
Click on continue and create a new folder naming it as "Helpers".
Then, create 2 new classes in the folder, naming them as "ImageHelper" and "FileHelper".
Again, Create a new folder naming it as "Models" and create a file naming it as "Contact.cs".
Add the following code to Contact.cs in order to store the contact.
Don't Forget to add system.collections.generic to the namespace (add the following code to the top of the file "using System.Collections.Generic;").
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FormattedName { get; set; }
public string Organization { get; set; }
public string Email { get; set; }
public string Title { get; set; }
public string Photo { get; set; }
public List<Phone> Phones { get; set; }
public List<Address> Addresses { get; set; }
}
public class Phone
{
public string Number { get; set; }
public string Type { get; set; }
}
public class Address
{
public string Description { get; set; }
public string Type { get; set; }
}
Now open ImageHelper.cs and make it as a static class and add the following code to convert the image URL to base64 format.
Open Program.cs and clear all the code in the Main method.
Add the following Namespaces to your Program.cs file.
using VCard.Models;
using VCard.Helpers;
Now we can create a new contact with its parameters..
Contact contact = new Contact
{
FirstName = "Satvik",
LastName = "Rajnarayanan",
Addresses = new System.Collections.Generic.List<Address>()
{
{ new Address(){ Description="This is my Home Address..", Type="Home"} },
{ new Address(){ Description="This is my Work Address", Type="Work"} }
},
Phones = new System.Collections.Generic.List<Phone>()
{
{ new Phone(){ Number="0000000000", Type="Home"} },
{ new Phone(){ Number="0000000000", Type="Work"} }
},
Email = "[email protected]",
Organization = "CSharpCorner",
Photo = "https://www.c-sharpcorner.com/App_Themes/CSharp/Images/SiteLogo.png",
Title = "Satvik"
};
Now we have set the parameters.
We can get the vcard as a string using the following command.
Join the conversation! Your thoughts help the community grow.