PBO to VCard

.PBO adalah file yang berisi kontak telpon. Biasanya digunakan pada windows. Sedangkan VCard adalah file kontak telpon yang biasanya dipakai pada HP. PBO sendiri berisi sekumpulan data VCard. Maka .PBO bisa kita pisahkan menjadi VCard. Berikut codenya.

using System;
using System.Collections.Generic;
using System.IO;

namespace pboToVcardConsole
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Hello World!");
			
			// TODO: Implement Functionality Here
			
			Console.WriteLine("Insert .PBO input path: ");
			string pathPBO = Console.ReadLine();
			Console.WriteLine("Output path:");
			string pathoutput = Console.ReadLine();
			
			using (StreamReader reader = new StreamReader(pathPBO))
			{
				int counterFile = 0;
	    		string line;
	    		string writeBuffer = "BEGIN:VCARD" + Environment.NewLine; 
	    		while ((line = reader.ReadLine()) != null)
	    		{
	    			Console.WriteLine(line);
	    			
	    			if((string.Equals(line,"END:VCARDBEGIN:VCARD"))||(string.Equals(line,"END:VCARD")))
	    			{
	    				if(!(writeBuffer.Length < 1))
	    				{
	    					writeBuffer = writeBuffer + "END:VCARD";
	    					
	    					if(!Directory.Exists(pathoutput))//if file doesn't exist
							{
								Directory.CreateDirectory(@pathoutput);
							}
	    					
	    					string namaFile = string.Format("{0}\\{1}.vcf",pathoutput,counterFile);
	    					if(!File.Exists(namaFile))//if file doesn't exist
							{
								File.Create(namaFile).Close();
							}
	    					TextWriter tw = new StreamWriter(@namaFile);
							tw.WriteLine(writeBuffer);
							tw.Close();
							counterFile++;
							writeBuffer = "BEGIN:VCARD" + Environment.NewLine;
	    				}	
	    			}
	    			else
	    			{
	    				writeBuffer = writeBuffer + line + Environment.NewLine;
	    			}
				}
			}
		}
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s