【问题标题】:How to save contacts in the .VCF format如何以 .VCF 格式保存联系人
【发布时间】:2011-09-16 14:35:31
【问题描述】:

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

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 文件中。

【问题讨论】:

    标签: c# c#-4.0


    【解决方案1】:

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

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

    之后,您可以使用 File 类型的静态 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 编码。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多