简体   繁体   English

如何以.VCF格式保存联系人

[英]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. 我想将这些数据保存在本地磁盘的.VCF文件中。

Just create a StringBuilder instance and write the contents of the .VCF to it. 只需创建一个StringBuilder实例,然后将.VCF的内容写入该实例即可。

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. 之后,您可以使用File类型的静态WriteAllText (...)方法将其保存到文件中。

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

Just open a .vcf file with a text editor to explore its contents. 只需使用文本编辑器打开.vcf文件即可浏览其内容。 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. 如果要包括图像,则必须基于64位对其进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM