Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

c# - How to save contacts in the .VCF format

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...