ebook img

Visual C# 2010 Recipes No Cover PDF

1015 Pages·2010·5.06 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Visual C# 2010 Recipes No Cover

Visual C# 2010 Recipes A Problem-Solution Approach ■ ■ ■ Allen Jones and Adam Freeman Visual C# 2010 Recipes: A Problem-Solution Approach Copyright © 2010 by Allen Jones and Adam Freeman All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2525-6 ISBN-13 (electronic): 978-1-4302-2526-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Publisher and President: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: Mark Collins Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editor: Damon Larson Production Support: Patrick Cunningham Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. For my lovely wife Lena, and our three wonderful girls, Anya, Alexia, and Angelina. I love you all. —Allen Jones For my wife, Jacqui Griffyth, who I love a great deal. —Adam Freeman Contents at a Glance About the Author ...................................................................................................... xx About the Technical Reviewers ............................................................................... xxi Acknowledgments .................................................................................................. xxii Introduction ........................................................................................................... xxiii ■ Chapter 1: Application Development ....................................................................... 1 ■ Chapter 2: Data Manipulation ................................................................................ 53 ■ Chapter 3: Application Domains, Reflection, and Metadata ................................ 103 ■ Chapter 4: Threads, Processes, and Synchronization ......................................... 149 ■ Chapter 5: Files, Directories, and I/O ................................................................... 205 ■ Chapter 6: XML Processing ................................................................................. 261 ■ Chapter 7: Windows Forms.................................................................................. 307 ■ Chapter 8: Graphics, Multimedia, and Printing .................................................. 369 ■ Chapter 9: Database Access ................................................................................ 423 ■ Chapter 10: Networking ....................................................................................... 479 ■ Chapter 11: Security and Cryptography .............................................................. 543 ■ Chapter 12: Unmanaged Code Interoperability .................................................... 597 iv C ■ONTENTS AT A GLANCE ■ Chapter 13: Commonly Used Interfaces and Patterns ........................................ 619 ■ Chapter 14: Windows Integration ........................................................................ 683 ■ Chapter 15: Parallel Programming ...................................................................... 729 ■ Chapter 16: Using LINQ ........................................................................................ 749 ■ Chapter 17: Windows Presentation Foundation ................................................. 789 Index ....................................................................................................................... 905 v Contents About the Author ...................................................................................................... xx About the Technical Reviewers ............................................................................... xxi Acknowledgments .................................................................................................. xxii Introduction ........................................................................................................... xxiii ■ Chapter 1: Application Development ....................................................................... 1 1-1. Create a Console Application from the Command Line .............................................. 2 1-2. Create a Windows-Based Application from the Command Line ................................ 5 1-3. Create and Use a Code Module .................................................................................. 9 1-4. Create and Use a Code Library from the Command Line ......................................... 11 1-5. Access Command-Line Arguments .......................................................................... 12 1-6. Include Code Selectively at Build Time .................................................................... 14 1-7. Access a Program Element That Has the Same Name As a Keyword ...................... 18 1-8. Create and Manage Strongly Named Key Pairs ........................................................ 19 1-9. Give an Assembly a Strong Name ............................................................................ 21 1-10. Verify That a Strongly Named Assembly Has Not Been Modified ........................... 23 1-11. Delay-Sign an Assembly ........................................................................................ 24 1-12. Sign an Assembly with an Authenticode Digital Signature .................................... 26 1-13. Create and Trust a Test Software Publisher Certificate ......................................... 28 1-14. Manage the Global Assembly Cache ...................................................................... 29 1-15. Prevent People from Decompiling Your Code ......................................................... 30 vi ■ CONTENTS 1-16. Manipulate the Appearance of the Console ........................................................... 31 1-17. Create a Static Class .............................................................................................. 34 1-18. Create an Anonymous Type .................................................................................... 35 1-19. Create an ExpandoObject Dynamic Type................................................................ 37 1-20. Define an Automatically Implemented Property ..................................................... 39 1-21. Overload an Operator ............................................................................................. 41 1-22. Define a Conversion Operator ................................................................................ 44 1-23. Handle an Event with an Anonymous Function ...................................................... 46 1-24. Implement a Custom Indexer ................................................................................. 48 ■ Chapter 2: Data Manipulation ................................................................................ 53 2-1. Manipulate the Contents of a String Efficiently ........................................................ 54 2-2. Encode a String Using Alternate Character Encoding .............................................. 56 2-3. Convert Basic Value Types to Byte Arrays ................................................................ 59 2-4. Base64 Encode Binary Data ..................................................................................... 61 2-5. Validate Input Using Regular Expressions ................................................................ 65 2-6. Use Compiled Regular Expressions .......................................................................... 70 2-7. Create Dates and Times from Strings ...................................................................... 72 2-8. Add, Subtract, and Compare Dates and Times......................................................... 74 2-9. Sort an Array or a Collection .................................................................................... 77 2-10. Copy a Collection to an Array ................................................................................. 79 2-11. Use a Strongly Typed Collection ............................................................................. 81 2-12. Create a Generic Type ............................................................................................ 83 2-13. Store a Serializable Object to a File ....................................................................... 87 2-14. Serialize an Object Using JSON .............................................................................. 90 2-15. Read User Input from the Console.......................................................................... 93 vii ■ CONTENTS 2-16. Using Large Integer Values .................................................................................... 96 2-17. Select Collection or Array Elements ....................................................................... 97 2-18. Remove Duplicate Items from an Array or Collection ........................................... 100 ■ Chapter 3: Application Domains, Reflection, and Metadata ................................ 103 3-1. Create an Application Domain ................................................................................ 104 3-2. Create Types That Can Be Passed Across Application Domain Boundaries ........... 106 3-3. Avoid Loading Unnecessary Assemblies into Application Domains ....................... 109 3-4. Create a Type That Cannot Cross Application Domain Boundaries ........................ 111 3-5. Load an Assembly into the Current Application Domain ........................................ 111 3-6. Execute an Assembly in a Different Application Domain........................................ 114 3-7. Instantiate a Type in a Different Application Domain ............................................. 116 3-8. Pass Data Between Application Domains............................................................... 121 3-9. Unload Assemblies and Application Domains ........................................................ 124 3-10. Retrieve Type Information .................................................................................... 125 3-11. Test an Object’s Type ........................................................................................... 127 3-12. Instantiate an Object Using Reflection ................................................................. 129 3-13. Create a Custom Attribute .................................................................................... 133 3-14. Inspect the Attributes of a Program Element Using Reflection ............................ 136 3-15. Programmatically Discover the Members of a Type............................................. 137 3-16. Invoke a Type Member Using Reflection .............................................................. 140 3-17. Dynamically Invoke a Type Member .................................................................... 142 3-18. Create a Custom Dynamic Type ........................................................................... 143 ■ Chapter 4: Threads, Processes, and Synchronization ......................................... 149 4-1. Execute a Method Using the Thread Pool ............................................................... 151 4-2. Execute a Method Asynchronously ........................................................................ 154 viii ■ CONTENTS 4-3. Execute a Method Periodically ............................................................................... 163 4-4. Execute a Method at a Specific Time ..................................................................... 166 4-5. Execute a Method by Signaling a WaitHandle Object ............................................. 167 4-6. Execute a Method Using a New Thread ................................................................. 169 4-7. Synchronize the Execution of Multiple Threads Using a Monitor ........................... 172 4-8. Synchronize the Execution of Multiple Threads Using an Event ............................ 178 4-9. Synchronize the Execution of Multiple Threads Using a Mutex ............................. 182 4-10. Synchronize the Execution of Multiple Threads Using a Semaphore ................... 185 4-11. Synchronize Access to a Shared Data Value ........................................................ 187 4-12. Know When a Thread Finishes ............................................................................. 190 4-13. Terminate the Execution of a Thread ................................................................... 191 4-14. Create a Thread-Safe Collection Instance ............................................................ 193 4-15. Start a New Process ............................................................................................. 195 4-16. Terminate a Process ............................................................................................ 199 4-17. Ensure That Only One Instance of an Application Can Execute Concurrently ...... 201 ■ Chapter 5: Files, Directories, and I/O ................................................................... 205 5-1. Retrieve Information About a File, Directory, or Drive ............................................ 206 5-2. Set File and Directory Attributes ............................................................................ 211 5-3. Copy, Move, or Delete a File or Directory ............................................................... 213 5-4. Calculate the Size of a Directory ............................................................................ 216 5-5. Retrieve Version Information for a File ................................................................... 217 5-6. Show a Just-in-Time Directory Tree in the TreeView Control ................................ 219 5-7. Read and Write a Text File ..................................................................................... 222 5-8. Read and Write a Binary File .................................................................................. 224 5-9. Read a File Asynchronously ................................................................................... 226 ix ■ CONTENTS 5-10. Find Files That Match a Wildcard Expression....................................................... 230 5-11. Test Two Files for Equality ................................................................................... 231 5-12. Manipulate Strings Representing File Names ...................................................... 233 5-13. Determine If a Path Is a Directory or a File .......................................................... 235 5-14. Work with Relative Paths ..................................................................................... 236 5-15. Create a Temporary File ....................................................................................... 238 5-16. Get the Total Free Space on a Drive ..................................................................... 239 5-17. Show the Common File Dialog Boxes ................................................................... 241 5-18. Use an Isolated Store ........................................................................................... 243 5-19. Monitor the File System for Changes ................................................................... 246 5-20. Access a COM Port ............................................................................................... 248 5-21. Get a Random File Name ...................................................................................... 249 5-22. Manipulate the Access Control List of a File or Directory .................................... 250 5-23. Compress Data ..................................................................................................... 253 5-24. Log Data to a File ................................................................................................. 254 5-25. Process a Log File ................................................................................................ 256 5-26. Communicate Between Processes ....................................................................... 257 ■ Chapter 6: XML Processing ................................................................................. 261 6-1. Show the Structure of an XML Document in a TreeView ....................................... 261 6-2. Insert Nodes in an XML Document ......................................................................... 266 6-3. Quickly Append Nodes in an XML Document ......................................................... 268 6-4. Find Specific Elements by Name ............................................................................ 271 6-5. Get XML Nodes in a Specific XML Namespace....................................................... 272 6-6. Find Elements with an XPath Search ..................................................................... 274 6-7. Read and Write XML Without Loading an Entire Document into Memory .............. 278 x

Description:
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions 356. 7-21. Display WPF Windows in a Windows Forms Application . NET Framework class library, Visual C# 2010 Recipes is the perfect resource to assist you. However, you
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.