In this article I will explain with an example, how to create vCard (.VCF) file in Windows Forms (WinForms) Application using C# and VB.Net.
Image Folder (Directory) Location
The Image file is located inside the Images Folder of Windows project.
Form Design
The Form consists of following controls:
PictureBox – For displaying image.
Label – For labelling and displaying vCard details.
Button – For creating vCard.
The Button has been assigned with a Click event handler.
Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Text;
VB.Net
Imports System.IO
Imports System.Text
Creating vCard (.VCF) file using C# and VB.Net
When the Create VCF Button is clicked, first a StringBuilder class object is created.
Then, the information for creating the vCard (.VCF) is read from their respective fields and appended to the StringBuilder class object using Append method.
After that, the image file is converted into BYTE Array using ReadAllBytes method of File class.
The BYTE Array is then converted to BASE64 string which is ultimately appended to the StringBuilder class object.
Finally, the StringBuilder class object is written to the Stream which initiates the saves the vCard in the specified location.
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("E:\\Files\\Mudassar.vcf");
sw.Write(sb.ToString());
sw.Close();
}
VB.Net
Private Sub OnCreate(sender As Object, e As EventArgs) Handles btnCreateVCF.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("E:\Files\Mudassar.vcf")
sw.Write(sb.ToString())
sw.Close()
End Sub
Screenshots
Form
Downloaded vCard (.VCF) file
Downloads