In this article I will explain with an example, how to create vCard in Windows Forms (WinForms) Application using C# and VB.Net.
Form Design
The following form consists of a PictureBox, some Labels and a Button control.
Creating vCard in Windows Forms using C# and VB.Net
When the Create Button is clicked, the following event handler is executed.
Inside this event handler, first a StringBuilder class object is created. Then the information for creating the vCard (.VCF) is read from the Label controls and appended to the StringBuilder class object using Append function.
For the contact Photo, Image file is read from the Folder (Directory) and converted to Byte Array and then converted to BASE64 string and finally, appended to the StringBuilder object.
Once the vCard details such as Name, Organization, Phone Numbers, Email Addresses, Websites and Photo of the contact is filled, the file is saved in the Folder (Directory) using the StreamWriter class.
C#
private void OnCreate(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("BEGIN:VCARD\r\nVERSION:2.1");
sb.Append(Environment.NewLine);
sb.Append("N:");
sb.Append(lblName.Text.Split(' ')[0]);
sb.Append(";");
sb.Append(lblName.Text.Split(' ')[1]);
sb.Append(";");
sb.Append(Environment.NewLine);
sb.Append("FN:");
sb.Append(lblName.Text);
sb.Append(Environment.NewLine);
sb.Append("TEL;CELL:");
sb.Append(lblMobileNumber.Text);
sb.Append(Environment.NewLine);
sb.Append("TEL;HOME:");
sb.Append(lblHomeNumber.Text);
sb.Append(Environment.NewLine);
sb.Append("TEL;WORK:");
sb.Append(lblWorkNumber.Text);
sb.Append(Environment.NewLine);
sb.Append("EMAIL;WORK:");
sb.Append(lblEmail.Text);
sb.Append(Environment.NewLine);
sb.Append("ORG:");
sb.Append(lblOrganization.Text);
sb.Append(Environment.NewLine);
sb.Append("TITLE:");
sb.Append(lblTitle.Text);
sb.Append(Environment.NewLine);
sb.Append("URL:");
sb.Append(lblWebsite.Text);
sb.Append(Environment.NewLine);
byte[] bytes = File.ReadAllBytes(Application.StartupPath.Replace("\\bin\\Debug", "")+"\\Images\\Mudassar.png");
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
sb.Append("PHOTO;ENCODING=BASE64;JPEG:");
sb.Append(base64);
sb.Append(Environment.NewLine);
sb.Append("END:VCARD");
StreamWriter sw = new StreamWriter("D:\\Files\\Mudassar.vcf");
sw.Write(sb.ToString());
sw.Close();
}
VB.Net
Private Sub OnCreate(sender As Object, e As EventArgs) Handles btnCreate.Click
Dim sb As StringBuilder = New StringBuilder()
sb.Append("BEGIN:VCARD" & vbCrLf & "VERSION:2.1")
sb.Append(Environment.NewLine)
sb.Append("N:")
sb.Append(lblName.Text.Split(" "c)(0))
sb.Append(";")
sb.Append(lblName.Text.Split(" "c)(1))
sb.Append(";")
sb.Append(Environment.NewLine)
sb.Append("FN:")
sb.Append(lblName.Text)
sb.Append(Environment.NewLine)
sb.Append("TEL;CELL:")
sb.Append(lblMobileNumber.Text)
sb.Append(Environment.NewLine)
sb.Append("TEL;HOME:")
sb.Append(lblHomeNumber.Text)
sb.Append(Environment.NewLine)
sb.Append("TEL;WORK:")
sb.Append(lblWorkNumber.Text)
sb.Append(Environment.NewLine)
sb.Append("EMAIL;WORK:")
sb.Append(lblEmail.Text)
sb.Append(Environment.NewLine)
sb.Append("ORG:")
sb.Append(lblOrganization.Text)
sb.Append(Environment.NewLine)
sb.Append("TITLE:")
sb.Append(lblTitle.Text)
sb.Append(Environment.NewLine)
sb.Append("URL:")
sb.Append(lblWebsite.Text)
sb.Append(Environment.NewLine)
Dim bytes As Byte() = File.ReadAllBytes(Application.StartupPath.Replace("\bin\Debug", "") & "\Images\Mudassar.png")
Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
sb.Append("PHOTO;ENCODING=BASE64;JPEG:")
sb.Append(base64)
sb.Append(Environment.NewLine)
sb.Append("END:VCARD")
Dim sw As StreamWriter = New StreamWriter("D:\Files\Mudassar.vcf")
sw.Write(sb.ToString())
sw.Close()
End Sub
Demo
Downloads