Feedback

C# - Klasse zum Erstellen einer VCard für Outlook

Veröffentlicht von am 7/7/2007
(1 Bewertungen)
Klasse zum Erstellen einer VCard für Outlook
public class VCard
{
    private List<DeliveryAddress> addresses = new List<DeliveryAddress>();
    private List<String> emailAddresses = new List<String>();
    private List<TelephoneNumber> phoneNumber = new List<TelephoneNumber>();
    private DateTime birthDate = DateTime.MinValue;
    private string firstName;
    private string formattedName;
    private string lastName;
    private string middleName;
    private string namePrefix;
    private string nameSuffix;
    private string organization;
    private string role;
    private string title;

    /// <summary>
    /// Gets or sets the formatted name.
    /// </summary>
    /// <value>the formatted name.</value>
    public string FormattedName
    {
        get { return formattedName; }
        set { formattedName = value; }
    }

    /// <summary>
    /// Gets or sets the last name.
    /// </summary>
    /// <value>The last name.</value>
    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    /// <summary>
    /// Gets or sets the first name.
    /// </summary>
    /// <value>The first name.</value>
    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    /// <summary>
    /// Gets or sets the middle name.
    /// </summary>
    /// <value>The middle name.</value>
    public string MiddleName
    {
        get { return middleName; }
        set { middleName = value; }
    }

    /// <summary>
    /// Gets or sets the name prefix.
    /// </summary>
    /// <value>The name prefix.</value>
    public string NamePrefix
    {
        get { return namePrefix; }
        set { namePrefix = value; }
    }

    /// <summary>
    /// Gets or sets the name suffix.
    /// </summary>
    /// <value>The name suffix.</value>
    public string NameSuffix
    {
        get { return nameSuffix; }
        set { nameSuffix = value; }
    }

    /// <summary>
    /// Gets or sets the birth date.
    /// </summary>
    /// <value>The birth date.</value>
    public DateTime BirthDate
    {
        get { return birthDate; }
        set { birthDate = value; }
    }

    /// <summary>
    /// Gets or sets the addresses.
    /// </summary>
    /// <value>The addresses.</value>
    public List<DeliveryAddress> Addresses
    {
        get { return addresses; }
        set { addresses = value; }
    }

    /// <summary>
    /// Gets or sets the phone number.
    /// </summary>
    /// <value>The phone number.</value>
    public List<TelephoneNumber> PhoneNumber
    {
        get { return phoneNumber; }
        set { phoneNumber = value; }
    }

    /// <summary>
    /// Gets or sets the email addresses.
    /// </summary>
    /// <value>The email addresses.</value>
    public List<string> EmailAddresses
    {
        get { return emailAddresses; }
        set { emailAddresses = value; }
    }

    /// <summary>
    /// Gets or sets the title.
    /// </summary>
    /// <value>The title.</value>
    public string Title
    {
        get { return title; }
        set { title = value; }
    }

    /// <summary>
    /// Gets or sets the role.
    /// </summary>
    /// <value>The role.</value>
    public string Role
    {
        get { return role; }
        set { role = value; }
    }

    /// <summary>
    /// Gets or sets the organization.
    /// </summary>
    /// <value>The organization.</value>
    public string Organization
    {
        get { return organization; }
        set { organization = value; }
    }

    /// <summary>
    /// Generates the V card.
    /// </summary>
    /// <param name="filePath">The file path.</param>
    /// <param name="mode">The mode.</param>
    public void GenerateVCard(string filePath, FileMode mode)
    {
        FileStream fs = new FileStream(filePath, mode);

        GenerateVCard(fs);
    }

    /// <summary>
    /// Generates the V card.
    /// </summary>
    /// <param name="outputStream">The output stream.</param>
    public void GenerateVCard(Stream outputStream)
    {
        StreamWriter sw = new StreamWriter(outputStream, Encoding.Default);

        using (sw)
        {
            sw.Write(ToString());
        }
    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("BEGIN:VCARD" + Environment.NewLine);

        if (formattedName != null)
        {
            sb.Append("FN:" + formattedName + Environment.NewLine);
        }

        sb.Append("N:" + lastName + ";");
        sb.Append(firstName + ";");
        sb.Append(middleName + ";");
        sb.Append(namePrefix + ";");
        sb.Append(nameSuffix + Environment.NewLine);

        if (birthDate != DateTime.MinValue)
        {
            sb.Append("BDAY:" + birthDate.ToString("yyyyMMdd") + Environment.NewLine);
        }

        foreach (DeliveryAddress da in addresses)
        {
            sb.Append(da.ToString());
        }

        foreach (TelephoneNumber phone in phoneNumber)
        {
            sb.Append(phone.ToString());
        }

        foreach (string email in emailAddresses)
        {
            sb.Append("EMAIL; INTERNET:" + email + Environment.NewLine);
        }

        sb.Append("TITLE:" + title + Environment.NewLine);
        sb.Append("ROLE:" + role + Environment.NewLine);
        sb.Append("ORG:" + organization + Environment.NewLine);
        sb.Append("END:VCARD" + Environment.NewLine);

        return sb.ToString();
    }


    public class DeliveryAddress
    {
        private string country;
        private string extendedAddress;
        private string locality;
        private string postalCode;
        private string postOfficeAddress;
        private string region;
        private string street;
        public AddressType DeliveryAddressType;

        /// <summary>
        /// Gets or sets the post office address.
        /// </summary>
        /// <value>The post office address.</value>
        public string PostOfficeAddress
        {
            get { return postOfficeAddress; }
            set { postOfficeAddress = value; }
        }

        /// <summary>
        /// Gets or sets the extended address.
        /// </summary>
        /// <value>The extended address.</value>
        public string ExtendedAddress
        {
            get { return extendedAddress; }
            set { extendedAddress = value; }
        }

        /// <summary>
        /// Gets or sets the street.
        /// </summary>
        /// <value>The street.</value>
        public string Street
        {
            get { return street; }
            set { street = value; }
        }

        /// <summary>
        /// Gets or sets the locality.
        /// </summary>
        /// <value>The locality.</value>
        public string Locality
        {
            get { return locality; }
            set { locality = value; }
        }

        /// <summary>
        /// Gets or sets the region.
        /// </summary>
        /// <value>The region.</value>
        public string Region
        {
            get { return region; }
            set { region = value; }
        }

        /// <summary>
        /// Gets or sets the postal code.
        /// </summary>
        /// <value>The postal code.</value>
        public string PostalCode
        {
            get { return postalCode; }
            set { postalCode = value; }
        }

        /// <summary>
        /// Gets or sets the country.
        /// </summary>
        /// <value>The country.</value>
        public string Country
        {
            get { return country; }
            set { country = value; }
        }

        /// <summary>
        /// Gibt einen <see cref="T:System.String"></see> zurück, der den aktuellen <see cref="T:System.Object"></see> darstellt.
        /// </summary>
        /// <returns>
        /// Ein <see cref="T:System.String"></see>, der den aktuellen <see cref="T:System.Object"></see> darstellt.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("ADR;" + Enum.GetName(typeof(AddressType), DeliveryAddressType) + ":");
            sb.Append(postOfficeAddress + ";");
            sb.Append(extendedAddress + ";");
            sb.Append(street + ";");
            sb.Append(locality + ";");
            sb.Append(region + ";");
            sb.Append(postalCode + ";");
            sb.Append(country + Environment.NewLine);

            return sb.ToString();
        }
    }

    public enum AddressType
    {
        DOM,
        INTL,
        POSTAL,
        PARCEL,
        HOME,
        WORK
    }


    public class TelephoneNumber
    {
        private string _phoneNumber;

        public TelephoneNumberType PhoneNumberType;

        /// <summary>
        /// Gets or sets the phone number.
        /// </summary>
        /// <value>The phone number.</value>
        public string PhoneNumber
        {
            get { return _phoneNumber; }
            set { _phoneNumber = value; }
        }

        /// <summary>
        /// Gibt einen <see cref="T:System.String"></see> zurück, der den aktuellen <see cref="T:System.Object"></see> darstellt.
        /// </summary>
        /// <returns>
        /// Ein <see cref="T:System.String"></see>, der den aktuellen <see cref="T:System.Object"></see> darstellt.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("TEL;" + Enum.GetName(typeof(TelephoneNumberType), PhoneNumberType) + ":");
            sb.Append(_phoneNumber + Environment.NewLine);

            return sb.ToString();
        }
    }

    public enum TelephoneNumberType
    {
        PREF,
        WORK,
        HOME,
        VOICE,
        FAX,
        MSG,
        CELL,
        PAGER,
        BBS,
        MODEM,
        CAR
    }
}
Abgelegt unter VCard, Outlook, VCalendar.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!