Question

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

Was it helpful?

Solution

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

OTHER TIPS

You can try to use this sample.

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

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.

C# purely does not has specific method or class for work with vcfs. But these solutions can be way.last one is the best:

1.Work with Microsot.office.interop.outlook. Ohhh nooo.it will open outlook first and then after steps you can do your work.surely its not best solution.

2.Use Thought vcard.its not too bad but image that you have multiple vcfs and you merge them in one vcf.with this package its too hard(or almost impossible) to work with it.

3.Use VCardlib .it doesnt has previous problem but its not update for work with vcf version4 until now.

  1. I use [MixERP.NET.VCard][۳] and its more simple and effective.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top