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

标签 c# c#-4.0

我有一个类来保存数据和该类的列表。 这是我的代码。

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; }
}

我想将这些数据保存在本地磁盘的 .VCF 文件中。

最佳答案

只需创建一个 StringBuilder 实例并将 .VCF 的内容写入其中即可。

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

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

之后您可以使用静态 WriteAllText 将其保存到文件中(...) 文件类型的方法。

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

只需使用文本编辑器打开 .vcf 文件即可浏览其内容。由于您只需要几个属性,因此应该很容易弄清楚。

一个小例子:

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

如果你想包含一张图片,你必须对其进行 base 64 编码。

关于c# - 如何以 .VCF 格式保存联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7446295/

相关文章:

c# - 在 C# 中将对象集合转换为 JavaScript 数组的创造性方法

c# - 在 Reflection.Emit 中使用发射类型作为类型参数

c# - 为什么我可以放弃 IList<T> 的不变性?

c# - 使用 PetaPoco Dynamic 和 WebAPI 返回 Json

c# - 如何从 "System.Drawing.Color"字符串中获取 System.Type

c# - 字典是否可以将数组作为键?

c# - 如何取消 Stream.ReadAsync?

c# - 使用正则表达式拆分字符串

具有尾递归优化的 C# 编译?

c# - 如何在用户书写时在 RichTextBox 中用不同的颜色为不同的单词着色,并在单击该彩色文本时引发事件