VCF Vcard import c#

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 03:36:54

问题


iam trying to write a class which imports *.vcf files (Vcard), because i didn´t found a adequate .net class to solve that job.

So i decided to treat the *.vcf file like a *.txt file. I just import the whole file, line by line, with a StreamReader. Finally i save the line into a List object.

The Code:

 private List<string> vcardList = new List<String>();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        using (StreamReader reader = new StreamReader(@"H:\VS.vcf"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                vcardList.Add(line);
            }
        }
    }

After importing the text i needed to edit the lines, because i need to remove all the unnecessary symbols. I tried to use the RedEx claa:

private void button1_Click(object sender, EventArgs e)
    {
        vcardList[0] = Regex.Replace(vcardList[0], "BEGIN:", string.Empty);          
    } 

that works very well, for the first line! but the *.vcf file is very complex and allways different.

So my question is: Is there a better way to solve that problem?

This is the *.vcf file:

BEGIN:VCARD
VERSION:2.1
N;LANGUAGE=de;CHARSET=Windows-1252:Test;Mustermann;;;(geschäftlich)
FN;CHARSET=Windows-1252:Test Mustermann (geschäftlich)
ORG:Mustermann CompanyTITLE;CHARSET=Windows-1252:CEO
TEL;WORK;VOICE:0049 1111 22 769 23 - 1
TEL;CELL;VOICE:0049 2222 33 71 55 90
ADR;WORK;PREF;CHARSET=Windows-1252:;;Frobuehl 22;Gothtown;;101092;England
LABEL;WORK;PREF;CHARSET=Windows-1252;ENCODING=QUOTED-PRINTABLE:Leihb=FChl 21=0D=0A=
101092 Frobuehl 
X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
URL;HOME:www.Test-Mustermann.de
EMAIL;PREF;INTERNET:Test@Test-Mustermann.de
X-MS-OL-DESIGN;CHARSET=utf-8:<card 
END:VCARD

I only need the name and address. Thanks in advance


回答1:


Pretty old, but it still works: https://github.com/drlongnecker/Thought.vCards




回答2:


You can try to use this sample.




回答3:


Helle, sry for the long delay.

I solved my problem now with the follwing code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
   {
        Outlook.ContactItem contact;
        Outlook.Application app = new Outlook.Application();

        contact = (Outlook.ContactItem)app.Session.OpenSharedItem(@"C:\vv.vfc");
        MessageBox.Show(contact.FirstName);
    }
   }
 }

This is just an example of how to easily import a VCF file with C# . i hope this helps. Of course i wouldnt implement it in that way, i rather would create a method with a parameter "incomingFile" or smth else.

Berry




回答4:


the solution with the outlook application is not effictive.

Outlook.Application app = new Outlook.Application();

this will open the outlook first. and actually you need only to open the vcard and parse it to extract the information you want from it.



来源:https://stackoverflow.com/questions/7593309/vcf-vcard-import-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!