Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save contacts in the .VCF format

Tags:

c#

c#-4.0

I have a class to hold data and a list of that class. Here is my code.

static void Main(string[] args)
    {
        List<GoogleContacts> contacts = new List<GoogleContacts>();
        contacts.Add(new GoogleContacts { title = "A", email = "B", im = "X" });
        contacts.Add(new GoogleContacts { title = "C", email = "D", im = "Y" });
        contacts.Add(new GoogleContacts { title = "E", email = "F", im = "Z" });
    }
}


public class GoogleContacts
{
    public string title { get; set; }
    public string email { get; set; }
    public string im { get; set; }
}

I want to save those data in a .VCF file in local Disk.

like image 696
Vero009 Avatar asked Sep 16 '11 14:09

Vero009


People also ask

How do I save contacts as VCF?

On your Android phone or tablet, open the Contacts app . At the bottom, tap Fix & manage Import from file. If you have multiple accounts on your device, pick the account where you want to save the contacts. Find and select the VCF file to import.

How do I share contacts as VCF?

Transfer contacts from Android to Android via VCF fileOpen the “Contacts” app on your old phone. Open the hamburger menu (three horizontal lines at the top of the screen) and tap “Manage contacts”. Tap the option to export contacts and then save them as a vCard file on your phone (internal storage).

Where does the VCF file save in Android?

vcf file is now stored on your Google Drive, and you have created a backup for your contacts.


1 Answers

Just create a StringBuilder instance and write the contents of the .VCF to it.

var contact = new GoogleContacts() { ... };

var vcf = new StringBuilder();
vcf.Append("TITLE:" + contact.Title + System.Environment.NewLine); 
//...

Afterwards you can save it to a file using the static WriteAllText(...) method of the File type.

var filename = @"C:\mycontact.vcf";
File.WriteAllText(filename, vcf.ToString());

Just open a .vcf file with a text editor to explore its contents. Since you only require a couple of properties it should be easy to figure out.

A small example:

BEGIN:VCARD
FN:Mr. John Smith
TITLE:Developer
ORG:Microsoft
BDAY:1979-12-10
VERSION:2.1
END:VCARD

If you want to include an image you have to base 64 encode it.

like image 143
Christophe Geers Avatar answered Sep 27 '22 19:09

Christophe Geers