1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2008 Google Inc. All rights reserved. 4 // 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file or at 7 // https://developers.google.com/open-source/licenses/bsd 8 #endregion 9 10 using System; 11 using System.IO; 12 13 namespace Google.Protobuf.Examples.AddressBook 14 { 15 internal class ListPeople 16 { 17 /// <summary> 18 /// Iterates though all people in the AddressBook and prints info about them. 19 /// </summary> Print(AddressBook addressBook)20 private static void Print(AddressBook addressBook) 21 { 22 foreach (Person person in addressBook.People) 23 { 24 Console.WriteLine("Person ID: {0}", person.Id); 25 Console.WriteLine(" Name: {0}", person.Name); 26 if (person.Email != "") 27 { 28 Console.WriteLine(" E-mail address: {0}", person.Email); 29 } 30 31 foreach (Person.Types.PhoneNumber phoneNumber in person.Phones) 32 { 33 switch (phoneNumber.Type) 34 { 35 case Person.Types.PhoneType.Mobile: 36 Console.Write(" Mobile phone #: "); 37 break; 38 case Person.Types.PhoneType.Home: 39 Console.Write(" Home phone #: "); 40 break; 41 case Person.Types.PhoneType.Work: 42 Console.Write(" Work phone #: "); 43 break; 44 } 45 Console.WriteLine(phoneNumber.Number); 46 } 47 } 48 } 49 50 /// <summary> 51 /// Entry point - loads the addressbook and then displays it. 52 /// </summary> Main(string[] args)53 public static int Main(string[] args) 54 { 55 if (args.Length != 1) 56 { 57 Console.Error.WriteLine("Usage: ListPeople ADDRESS_BOOK_FILE"); 58 return 1; 59 } 60 61 if (!File.Exists(args[0])) 62 { 63 Console.WriteLine("{0} doesn't exist. Add a person to create the file first.", args[0]); 64 return 0; 65 } 66 67 // Read the existing address book. 68 using (Stream stream = File.OpenRead(args[0])) 69 { 70 AddressBook addressBook = AddressBook.Parser.ParseFrom(stream); 71 Print(addressBook); 72 } 73 return 0; 74 } 75 } 76 }