private static void InterpretLineElement(string line, ref VCard card) { int end = -1, begin = -1; end = line.IndexOf(Enums.VCardMetaData.KeyValueSeperator); begin = end + 1; string val = line.Substring(begin); if (val != null) { val = val.Replace(Enums.File.CsvDelimiter, " "); } if(line.StartsWith(Enums.VCardElements.VERSION)) { card.VERSION = card.VERSION ?? val; } else if (line.StartsWith(Enums.VCardElements.N)) { card.N = card.N ?? val; } else if (line.StartsWith(Enums.VCardElements.FN)) { card.FN = card.FN ?? val; } else if (line.StartsWith(Enums.VCardElements.EMAIL)) { card.EMAIL = card.EMAIL ?? val; } else if (line.StartsWith(Enums.VCardElements.TEL)) { if (card.TEL == null) { card.TEL = val; } else { card.TEL += Enums.VCardMetaData.ValueJoinDelimiter + val; } } else if (line.StartsWith(Enums.VCardElements.ORG)) { card.ORG = card.ORG ?? val; } else if (line.StartsWith(Enums.VCardElements.PHOTO)) { card.PHOTO = card.PHOTO ?? val; } else if (line.StartsWith(Enums.VCardElements.ADR)) { card.ADR = card.ADR ?? val; } else if (line.StartsWith(Enums.VCardElements.LABEL)) { card.LABEL = card.LABEL ?? val; } else if (line.StartsWith(Enums.VCardElements.REV)) { card.REV = card.REV ?? val; } }
private static void CreateVCards(List<string> fileLines) { for(int index=0; index<Math.Min(BeginLines.Count, EndLines.Count); index++) { VCard card = new VCard(); for (int index2 = BeginLines[index]; index2 < EndLines[index]; index2++) { InterpretLineElement(fileLines[index2], ref card); } card.BEGIN = fileLines[BeginLines[index]]; card.END = fileLines[EndLines[index]]; vCards.Add(card); } }
/// <summary> /// This method will read the given <paramref name="inputFile"/> convert it to HTML and write it to the <paramref name="outputFolder"/> /// </summary> /// <param name="inputFile">The vcf file</param> /// <param name="outputFolder">The folder where to save the converted vcf file</param> /// <param name="hyperlinks">When true hyperlinks are clickable, otherwhise they are written as plain text</param> /// <returns>String array containing the full path to the converted VCF file</returns> /// <exception cref="ArgumentNullException">Raised when the <paramref name="inputFile" /> or <paramref name="outputFolder" /> is null or empty</exception> /// <exception cref="FileNotFoundException">Raised when the <paramref name="inputFile" /> does not exists</exception> /// <exception cref="DirectoryNotFoundException">Raised when the <paramref name="outputFolder" /> does not exist</exception> /// <exception cref="VCRFileTypeNotSupported">Raised when the extension is not .vcf</exception> public string[] ExtractToFolder(string inputFile, string outputFolder, bool hyperlinks = false) { outputFolder = FileManager.CheckForBackSlash(outputFolder); _errorMessage = string.Empty; CheckFileNameAndOutputFolder(inputFile, outputFolder); using (TextReader textReader = File.OpenText(inputFile)) { var vcardReader = new VCardReader(); var vCard = new VCard(); vcardReader.ReadInto(vCard, textReader); return(WriteVCard(vCard, outputFolder, hyperlinks).ToArray()); } }
private static void SaveCardsAsCSV() { var dirInfo = Directory.GetParent(ContactFileName); var dirName = dirInfo.ToString(); string filePath = string.Format(@"{0}\\{1}.{2}", dirName,Enums.File.OutputName,Enums.File.CsvFormat); string delimiter = Enums.File.CsvDelimiter; string header = null; var c = new VCard(); foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(c)) { header += descriptor.Name + delimiter; } StringBuilder sb = new StringBuilder(); sb.AppendLine(header); var contactLines = new List<string>(); foreach (VCard card in vCards) { string contactLine = null; foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(card)) { object value = descriptor.GetValue(card); if (value != null) { contactLine += value.ToString() + delimiter; } else { contactLine += "" + delimiter; } } contactLines.Add(contactLine); } foreach (string line in contactLines) { sb.AppendLine(line); } File.WriteAllText(filePath, sb.ToString()); Console.WriteLine(string.Format("All contents successfully written to {0}", filePath)); }
/// <summary> /// Writes the body of the MSG Contact to html or text and extracts all the attachments. The /// result is return as a List of strings /// </summary> /// <param name="vCard"> /// <see cref="VCard" /> /// </param> /// <param name="outputFolder">The folder where we need to write the output</param> /// <param name="hyperlinks">When true then hyperlinks are generated for the To, CC, BCC and attachments</param> /// <returns></returns> private List <string> WriteVCard(VCard vCard, string outputFolder, bool hyperlinks) { var fileName = Path.Combine(outputFolder, "contact.html"); var files = new List <string> { fileName }; var output = new StringBuilder(); // Start of table WriteTableStart(output); var i = 1; var count = vCard.Photos.Count; foreach (var photo in vCard.Photos) { string photoLabel; if (count > 1) { photoLabel = LanguageConsts.PhotoLabel + " " + i; } else { photoLabel = LanguageConsts.PhotoLabel; } if (photo.IsLoaded) { var tempFileName = Path.Combine(outputFolder, Guid.NewGuid() + ".png"); var bitmap = photo.GetBitmap(); bitmap.Save(tempFileName, ImageFormat.Png); files.Add(tempFileName); WriteTableRowImage(output, photoLabel, tempFileName); } else { if (hyperlinks) { WriteTableRowHyperLink(output, photoLabel, photo.Url.ToString(), photo.Url.ToString()); } else { WriteTableRowImage(output, photoLabel, photo.Url.ToString()); } } i += 1; } // Full name if (!string.IsNullOrEmpty(vCard.FormattedName)) { WriteTableRow(output, LanguageConsts.DisplayNameLabel, vCard.FormattedName); } // Last name if (!string.IsNullOrEmpty(vCard.FamilyName)) { WriteTableRow(output, LanguageConsts.SurNameLabel, vCard.FamilyName); } // First name if (!string.IsNullOrEmpty(vCard.GivenName)) { WriteTableRow(output, LanguageConsts.GivenNameLabel, vCard.GivenName); } // Job title if (!string.IsNullOrEmpty(vCard.Title)) { WriteTableRow(output, LanguageConsts.FunctionLabel, vCard.Title); } // Department if (!string.IsNullOrEmpty(vCard.Department)) { WriteTableRow(output, LanguageConsts.DepartmentLabel, vCard.Department); } // Company if (!string.IsNullOrEmpty(vCard.Organization)) { WriteTableRow(output, LanguageConsts.CompanyLabel, vCard.Organization); } // Empty line WriteEmptyTableRow(output); if (vCard.DeliveryLabels.Count == 0) { foreach (var deliveryAddress in vCard.DeliveryAddresses) { var address = (!string.IsNullOrWhiteSpace(deliveryAddress.Street) ? deliveryAddress.Street : string.Empty) + Environment.NewLine + (!string.IsNullOrWhiteSpace(deliveryAddress.PostalCode) ? deliveryAddress.PostalCode : string.Empty) + " " + (!string.IsNullOrWhiteSpace(deliveryAddress.City) ? deliveryAddress.City : string.Empty) + Environment.NewLine + (!string.IsNullOrWhiteSpace(deliveryAddress.Region) ? deliveryAddress.Region : string.Empty) + Environment.NewLine + (!string.IsNullOrWhiteSpace(deliveryAddress.Country) ? deliveryAddress.Country : string.Empty); // intl,postal,parcel,work if (deliveryAddress.IsWork) { WriteTableRow(output, LanguageConsts.WorkAddressLabel, address); } else if (deliveryAddress.IsHome) { WriteTableRow(output, LanguageConsts.HomeAddressLabel, address); } else if (deliveryAddress.IsInternational) { WriteTableRow(output, LanguageConsts.InternationalAddressLabel, address); } else if (deliveryAddress.IsPostal) { WriteTableRow(output, LanguageConsts.PostalAddressLabel, address); } else if (deliveryAddress.IsParcel) { WriteTableRow(output, LanguageConsts.ParcelAddressLabel, address); } else if (deliveryAddress.IsDomestic) { WriteTableRow(output, LanguageConsts.DomesticAddressLabel, address); } } } // Business address foreach (var deliveryLabel in vCard.DeliveryLabels) { switch (deliveryLabel.AddressType) { case DeliveryAddressTypes.Domestic: WriteTableRow(output, LanguageConsts.DomesticAddressLabel, deliveryLabel.Text); break; case DeliveryAddressTypes.Home: WriteTableRow(output, LanguageConsts.HomeAddressLabel, deliveryLabel.Text); break; case DeliveryAddressTypes.International: WriteTableRow(output, LanguageConsts.InternationalAddressLabel, deliveryLabel.Text); break; case DeliveryAddressTypes.Parcel: WriteTableRow(output, LanguageConsts.PostalAddressLabel, deliveryLabel.Text); break; case DeliveryAddressTypes.Postal: WriteTableRow(output, LanguageConsts.PostalAddressLabel, deliveryLabel.Text); break; case DeliveryAddressTypes.Work: WriteTableRow(output, LanguageConsts.WorkAddressLabel, deliveryLabel.Text); break; case DeliveryAddressTypes.Default: WriteTableRow(output, LanguageConsts.OtherAddressLabel, deliveryLabel.Text); break; } } // Instant messaging if (!string.IsNullOrEmpty(vCard.InstantMessagingAddress)) { WriteTableRow(output, LanguageConsts.InstantMessagingAddressLabel, vCard.InstantMessagingAddress); } //// Empty line WriteEmptyTableRow(output); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Work, PhoneTypes.WorkVoice }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Assistant, PhoneTypes.VoiceAssistant }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Company, PhoneTypes.VoiceCompany }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Home, PhoneTypes.HomeVoice }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Cellular, PhoneTypes.CellularVoice }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Car, PhoneTypes.CarVoice }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Radio, PhoneTypes.VoiceRadio }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Pager, PhoneTypes.VoicePager }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Callback, PhoneTypes.VoiceCallback }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Voice }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Preferred }); // Telex foreach (var email in vCard.EmailAddresses) { switch (email.EmailType) { case EmailAddressType.Telex: WriteTableRow(output, LanguageConsts.TelexNumberLabel, email.ToString()); break; } } WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Ttytdd }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Isdn }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.Fax }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.WorkFax }); WriteTelephone(vCard, output, new List <PhoneTypes> { PhoneTypes.HomeFax }); // Empty line WriteEmptyTableRow(output); i = 1; foreach (var email in vCard.EmailAddresses) { switch (email.EmailType) { case EmailAddressType.AOl: case EmailAddressType.AppleLink: case EmailAddressType.AttMail: case EmailAddressType.CompuServe: case EmailAddressType.EWorld: case EmailAddressType.IBMMail: case EmailAddressType.Internet: case EmailAddressType.MCIMail: case EmailAddressType.PowerShare: case EmailAddressType.Prodigy: if (i > 1) { WriteEmptyTableRow(output); } if (hyperlinks) { WriteTableRowHyperLink(output, LanguageConsts.EmailEmailAddressLabel + " " + i, "mailto:" + email, email.ToString()); } else { WriteTableRowNoEncoding(output, LanguageConsts.EmailEmailAddressLabel + " " + i, email.ToString()); } if (!string.IsNullOrEmpty(vCard.FormattedName)) { WriteTableRow(output, LanguageConsts.EmailDisplayNameLabel + " " + i, vCard.FormattedName + " (" + email + ")"); } i += 1; break; case EmailAddressType.Telex: // Ignore break; } } // Empty line WriteEmptyTableRow(output); // Birthday if (vCard.BirthDate != null) { WriteTableRow(output, LanguageConsts.BirthdayLabel, ((DateTime)vCard.BirthDate).ToString(LanguageConsts.DataFormat)); } // Anniversary if (vCard.Anniversary != null) { WriteTableRow(output, LanguageConsts.WeddingAnniversaryLabel, ((DateTime)vCard.Anniversary).ToString(LanguageConsts.DataFormat)); } // Spouse/Partner if (!string.IsNullOrEmpty(vCard.Spouse)) { WriteTableRow(output, LanguageConsts.SpouseNameLabel, vCard.Spouse); } // Profession if (!string.IsNullOrEmpty(vCard.Role)) { WriteTableRow(output, LanguageConsts.ProfessionLabel, vCard.Role); } // Assistant if (!string.IsNullOrEmpty(vCard.Assistant)) { WriteTableRow(output, LanguageConsts.AssistantTelephoneNumberLabel, vCard.Assistant); } // Web page var firstRow = true; foreach (var webpage in vCard.Websites) { if (!string.IsNullOrEmpty(webpage.Url)) { if (firstRow) { firstRow = false; if (hyperlinks) { WriteTableRowHyperLink(output, LanguageConsts.HtmlLabel, webpage.Url, webpage.Url); } else { WriteTableRow(output, LanguageConsts.HtmlLabel, webpage.Url); } } else { if (hyperlinks) { WriteTableRowHyperLink(output, string.Empty, webpage.Url, webpage.Url); } else { WriteTableRow(output, string.Empty, webpage.Url); } } } } // Empty line WriteEmptyTableRow(output); // Categories var categories = vCard.Categories; if (categories != null && categories.Count > 0) { WriteTableRow(output, LanguageConsts.CategoriesLabel, String.Join("; ", categories)); } // Empty line WriteEmptyTableRow(output); // Empty line firstRow = true; if (vCard.Notes != null && vCard.Notes.Count > 0) { foreach (var note in vCard.Notes) { if (!string.IsNullOrWhiteSpace(note.Text)) { if (firstRow) { firstRow = false; WriteTableRow(output, LanguageConsts.NotesLabel, note.Text); } else { WriteTableRow(output, string.Empty, note.Text); } } } } WriteTableEnd(output); // Write the body to a file File.WriteAllText(fileName, output.ToString(), Encoding.UTF8); return(files); }
/// <summary> /// Writes the selected <paramref name="phoneTypes"/> to the given <paramref name="output"/> for /// the selected <paramref name="vCard"/> /// </summary> /// <param name="vCard"></param> /// <param name="output"></param> /// <param name="phoneTypes"></param> private void WriteTelephone(VCard vCard, StringBuilder output, ICollection <PhoneTypes> phoneTypes) { var phones = vCard.Phones.Where(m => phoneTypes.Contains(m.PhoneType)).ToList(); var i = 0; var count = phones.Count; foreach (var phone in phones) { var labelSuffix = string.Empty; if (count > 0) { i += 1; if (i > 1) { labelSuffix = " " + i; } } switch (phone.PhoneType) { case PhoneTypes.Bbs: WriteTableRow(output, LanguageConsts.BBSTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Car: case PhoneTypes.CarVoice: WriteTableRow(output, LanguageConsts.CarTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Cellular: case PhoneTypes.CellularVoice: WriteTableRow(output, LanguageConsts.CellularTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Fax: WriteTableRow(output, LanguageConsts.OtherFaxLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.HomeFax: WriteTableRow(output, LanguageConsts.HomeFaxNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Home: case PhoneTypes.HomeVoice: WriteTableRow(output, LanguageConsts.HomeTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Isdn: WriteTableRow(output, LanguageConsts.ISDNNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.MessagingService: WriteTableRow(output, LanguageConsts.BeeperTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Modem: WriteTableRow(output, LanguageConsts.ModemTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Pager: case PhoneTypes.VoicePager: WriteTableRow(output, LanguageConsts.BeeperTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Ttytdd: WriteTableRow(output, LanguageConsts.TextTelephoneLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Preferred: WriteTableRow(output, LanguageConsts.PrimaryTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Video: WriteTableRow(output, LanguageConsts.VideoTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Voice: WriteTableRow(output, LanguageConsts.VoiceTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Work: case PhoneTypes.WorkVoice: WriteTableRow(output, LanguageConsts.BusinessTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Company: case PhoneTypes.VoiceCompany: WriteTableRow(output, LanguageConsts.CompanyMainTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Callback: case PhoneTypes.VoiceCallback: WriteTableRow(output, LanguageConsts.CallbackTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Radio: case PhoneTypes.VoiceRadio: WriteTableRow(output, LanguageConsts.RadioTelephoneNumberLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.Assistant: case PhoneTypes.VoiceAssistant: WriteTableRow(output, LanguageConsts.AssistantLabel + labelSuffix, phone.FullNumber); break; case PhoneTypes.WorkFax: WriteTableRow(output, LanguageConsts.BusinessFaxNumberLabel + labelSuffix, phone.FullNumber); break; } } }