Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
477 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...