Vous êtes sur la page 1sur 11

Table of Contents

Table of Contents ................................................................................................................................................................................. 1 BackCover .................................................................................................................................................................................................. 2 C# Programmer's Cookbook ....................................................................................................................................................... 3 Introduction ................................................................................................................................................................................................ 4 Code Samples ...................................................................................................................................................................................... 5 System Requirements ...................................................................................................................................................................... 6 Other Books .......................................................................................................................................................................................... 7 Microsoft Press Support .................................................................................................................................................................. 8 1.1 Create a Console Application ........................................................................................................................................... 9

C# Programmer's Cookbook @Team LiB

C# Programmer's Cookbook by Allen Jones ISBN:0735619301 Microsoft Press 2004 This book offers 226 code recipes applicable to a variety of difficulties that may arise in the process of application development. Topics covered include XML processing, Windows forms, database access, networking, runtime security, cryptography and more. Table of Contents C# Programmer's Cookbook Introduction Chapter 1 - Application Development Chapter 2 - Working with Data Chapter 3 - Application Domains, Reflection, and Metadata Chapter 4 - Threads, Processes, and Synchronization Chapter 5 - XML Processing Chapter 6 - Windows Forms Chapter 7 - ASP.NET and Web Forms Chapter 8 - Graphics, Multimedia, and Printing Chapter 9 - Files, Directories, and I/O Chapter 10 - Database Access Chapter 11 - Networking and Internetworking Chapter 12 - XML Web Services and Remoting Chapter 13 - Runtime Security Chapter 14 - Cryptography Chapter 15 - Unmanaged Code Interoperability Chapter 16 - Commonly Used Interfaces and Patterns Chapter 17 - Windows Integration Index List of Figures List of Tables

1 / 10

C# Programmer's Cookbook @Team LiB

Back Cover Next time you hit the wall with a tough C# development problem, get the code behind the solutionand solve it the right way. The C# Programmers Cookbook provides at-a-glance reference to hundreds of C# and Microsoft .NET Framework programming scenarios using a concise, problem/solution format. The books organized so you can quickly zero in on the topics and answers you needwith practical examples, code snippets, best practices, and undocumented secrets to get the job done. No half-baked solutions. Get expert code from expert developers. Get hundreds of recipes covering every application typefrom Microsoft Windows to Web pages, Web services, Windows Forms, and Windows services. Discover expert ways to solve common Web application and network programming challenges. Manage files, directories, and streams with the .NET I/O classes. Access databases using Microsoft ADO.NET. Tackle advanced techniques for multithreading, manipulating XML data, .NET Remoting, and reusing common patterns. Develop rich multimedia applications using the .NET Framework. Help protect your applications with code access security, role-based security, and cryptography. Interoperate with legacy Win32 API and COM-based solutions. About the Author Allen Jones is coauthor of the popular C# for Java Developers, Microsoft .NET XML Web Services Step by Step, and Programming .NET Security. A 13-year technology veteran, he is known for the depth of his C# and .NET expertise. He specializes in developing and implementing enterprise solutions, including e-commerce and security systems.

2 / 10

C# Programmer's Cookbook @Team LiB

C# Programmer's Cookbook
Allen Jones Microsoft

For Brenda
PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright 2004 by Allen Jones and Matthew MacDonald All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Cataloging-in-Publication Data [pending.] Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 QWT 8 7 6 5 4 3 Distributed in Canada by H.B. Fenn and Company Ltd. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to mspinput@microsoft.com. Active Directory, ActiveMovie, ActiveX, Authenticode, DirectShow, DirectX, Microsoft, Microsoft Press, MSDN, Visual Studio, Win32, Windows, the Windows logo, Windows Media, and Windows NT are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. Acquisitions Editor: Danielle Bird Voeller Project Editor: Dick Brown and Denise Bankaitis Technical Editor: Eric Dettinger Body Part No. X10-08418 Allen Jones Allen Jones is coauthor of the popular C# for Java Developers and Microsoft .NET XML Web Services Step by Step (Microsoft Press). A 13-year technology veteran, he is known for the depth of his C# and .NET expertise. Allen specializes in developing and implementing enterprise solutions, including e- commerce and security systems.

3 / 10

C# Programmer's Cookbook @Team LiB

Introduction
Overview Mastering the development of Microsoft .NET Framework applications in C# is less about knowing the C# language and more about knowing how to use the functionality of the .NET Framework class library most effectively. The C# Programmer's Cookbook explores the breadth of the .NET Framework class library and provides specific solutions to common and interesting programming problems. Each solution (or recipe) is presented in a succinct problem/solution format and is accompanied by working code samples. The C# Programmer's Cookbook is not intended to teach you how to program, nor to teach you C#. However, if you have even the most rudimentary experience programming applications built on the .NET Framework using C#, you will find this book to be an invaluable resource. Ideally, when you are facing a problem, this book will contain a recipe that provides the solutionor at least it will point you in the right direction. Even if you are simply looking to broaden your knowledge of the .NET Framework class library, the C# Programmer's Cookbook is the perfect resource to assist you. You cannot become proficient with C# and the classes in the .NET Framework class library merely by reading about them; you must use them and experiment with them by writing programs, programs, and more programs. The structure and content of this book and the real-world applicability of the solutions it provides offer the perfect starting point from which to kick-start your own experimentation. Note The code in this book has been written for and tested on version 1.1 of the .NET Framework. In many cases, you will find that the sample code will run on version 1.0 of the .NET Framework, but this has not been tested and no guarantees are made.

4 / 10

C# Programmer's Cookbook @Team LiB

Code Samples The code for all recipes in the C# Programmer's Cookbook is available online at http://microsoft.com/mspress/books/6456.asp. To download the sample files, click the Companion Content link in the More Information menu on the right side of the page. This action loads the Companion Content page, which includes a link for downloading the sample files. To install the sample files, click the Download The Book's Sample Files link and follow the instructions in the setup program. A link to the sample code will be added to your Start menu. The code is provided as a set of Visual Studio .NET 2003 solutions and projects organized by chapter and recipe number. Each chapter is a separate solution, and each recipe is a separate project within the chapter's solution. Some recipes in Chapter 11 and Chapter 12 that demonstrate network programming include separate projects that contain the client and server elements of the recipe's solution. Although all samples are provided as Visual Studio .NET projects, most consist of a single source file that you can compile and run independent of Visual Studio .NET. If you are not using Visual Studio .NET 2003 you can locate the code for a particular recipe by navigating through the directory structure of the sample code. For example, to find the code for recipe 4.3, you would look up the code in the directory "Chapter04\Recipe04-03". If you use the command-line compiler, ensure that you include references to all required .NET class library assemblies. Some of the sample applications require command-line arguments. Where required, the recipe's text will describe the arguments. If you are using Visual Studio .NET, you can enter these arguments in the project properties (under the Debugging node of the Configuration Properties item). Keep in mind that if you need to enter directory or file names that incorporate spaces, you will need to place the full name in quotation marks. Some additional steps are required to install the two virtual directories used for the examples that accompany Chapter 7, "ASP.NET and Web Forms," and Chapter 12, "XML Web Services and Remoting." These steps are described in a readme.txt file provided with the downloaded code and on the code download page.

5 / 10

C# Programmer's Cookbook @Team LiB

System Requirements To run the sample code accompanying this book, you will need the following software: Microsoft .NET Framework SDK version 1.1 Microsoft Visual Studio .NET 2003 (recommended) Microsoft Windows 2000, Windows XP, or Microsoft Windows Server 2003 Microsoft SQL Server 2000 or MSDE for the recipes in Chapter 10 Microsoft Internet Information Services (IIS) for some recipes in Chapter 7 and Chapter 12 The minimum hardware specification for development is a 450-MHz Pentium II-class processor, with a minimum of 128 MB of RAM if you're running Microsoft Windows 2000 and 256 MB of RAM if you're running Windows XP, Windows 2000 Server, or Windows Server 2003. You'll need about 5 GB of free hard-disk space to install Visual Studio .NET 2003. These values are minimums, and your development life will be much easier on a system with ample RAM and free disk space. Note Although Microsoft's implementation of the .NET Framework for Windows is the focus of the C# Programmer's Cookbook, an important goal was to provide a useful resource to all C# programmers regardless of the platform on which they are developing or the tools to which they have access. Apart from specific topics that are not supported on all .NET platforms (such as Windows Forms, ADO.NET, and ASP.NET) many of the samples in this book are valid across all .NET implementations.

6 / 10

C# Programmer's Cookbook @Team LiB

Other Books If you have no programming experience, this book will not teach you how to program. Similarly, if you are a programmer but have little or no C# experience, this book does not provide the structure required to teach you how to program effectively in C#. Depending on your programming background and current level of C# knowledge, the following books can teach you enough about C# and the .NET Framework to enable you to get the most from the solutions provided in this book:

Programming in the Key of C#: A Primer for Aspiring Programmers (Microsoft Press, 2003) Charles Petzold. Microsoft Visual C# .NET Step by Step Version 2003 (Microsoft Press, 2002) John Sharp and Jon Jagger. Microsoft Visual C# .NET (Core Reference) (Microsoft Press, 2002) Mickey Williams.
If you want to explore topics discussed in this book in more detail, or you want to understand more about the internal operation of the .NET Framework, I recommend the following books:

Inside C#, Second Edition (Microsoft Press, 2002) Tom Archer and Andrew Whitechapel. Applied Microsoft .NET Framework Programming (Microsoft Press, 2002) Jeffrey Richter (Wintellect). Essential .NET, Volume 1: The Common Language Runtime (Addison-Wesley, 2002) Don Box and Chris Sells. Programming Microsoft Windows with C# (Microsoft Press, 2001) Charles Petzold. Programming .NET Security (O'Reilly & Associates, 2003) Adam Freeman and Allen Jones. Microsoft ADO.NET (Core Reference) (Microsoft Press, 2002) David Sceppa. Advanced .NET Remoting (Apress, 2002) Ingo Rammer.

7 / 10

C# Programmer's Cookbook @Team LiB

Microsoft Press Support Every effort has been made to ensure the accuracy of the book and its companion content. Microsoft also provides corrections for books through the World Wide Web at the following address:
http://www.microsoft.com/mspress/support/

If you have comments, questions, or ideas regarding the presentation or use of this book or the companion content, you can send them to Microsoft using either of the following methods: Postal Mail: Microsoft Press ATTN: C# Programmer's Cookbook Editor One Microsoft Way Redmond, WA 98052-6399 E-Mail: mspinput@microsoft.com Please note that product support isn't offered through the above mail addresses. For support information regarding Visual Studio .NET 2003, go to http://msdn.microsoft.com/vstudio/. You can also call Standard Support at (425) 635-7011 weekdays between 6 A.M. and 6 P.M. Pacific time, or you can search Microsoft Product Support Services at http://support.microsoft.com/support.

8 / 10

C# Programmer's Cookbook @Team LiB

1.1 Create a Console Application

Problem
You need to build an application that doesn't require a Windows graphical user interface (GUI) but instead displays output to, or reads input from, the Windows command prompt (console).

Solution
Ensure you implement a static method named Main with one of the following signatures in at least one of your source code files. public static void Main(); public static void Main(string[] args); public static int Main(); public static int Main(string[] args); Use the /target:exe switch on the C# compiler (csc.exe) when you compile your assembly.

Discussion
By default, the C# compiler will build a Console application unless you specify otherwise. For this reason, it's not necessary to specify the /target:exe switch, but doing so makes your intention clearer, which is useful if you are creating build scripts that will be used by others or will be used repeatedly over a period of time. The following example lists a class named ConsoleUtils that is defined in a file named ConsoleUtils.cs: using System;
public class ConsoleUtils { // A method to display a prompt and read a response from the console public static string ReadString(string msg) { Console.Write(msg); return System.Console.ReadLine(); } // A method to display a message to the console public static void WriteString(string msg) { System.Console.WriteLine(msg); } // Main method used for testing ConsoleUtility methods public static void Main() { // Prompt the reader to enter their name string name = ReadString("Please enter your name : "); // Welcome the reader to the C# Cookbook WriteString("Welcome to the C# Programmer's Cookbook, " + name); } }

To build the ConsoleUtils class into a Console application named ConsoleUtils.exe, use the command csc /target:exe ConsoleUtils.cs. You can run the resulting executable assembly directly from the command line. When run, the Main method of the ConsoleUtils.exe application prompts you for your
9 / 10

C# Programmer's Cookbook @Team LiB

name and then welcomes you to the C# Programmer's Cookbook, as shown here. Please enter your name : Rupert Welcome to the C# Programmer's Cookbook, Rupert In reality, applications rarely consist of a single source file. As an example, the HelloWorld class listed here uses the ConsoleUtils class to display the message "Hello, world" to the console. (HelloWorld is contained in the HelloWorld.cs file.) public class HelloWorld {
public static void Main() { ConsoleUtils.WriteString("Hello, world"); } }

To build a Console application consisting of more than one source code file, you must specify all the source files as arguments to the compiler. For example, the following command builds an application named MyFirstApp.exe from the HelloWorld.cs and ConsoleUtils.cs source files. csc /target:exe /main:HelloWorld /out:MyFirstApp.exe HelloWorld.cs ConsoleUtils.cs The /out switch allows you to specify the name of the compiled assembly. Otherwise, the assembly is named after the first source file listedHelloWorld.cs in the example. Because both the HelloWorld and ConsoleUtils classes contain Main methods, the compiler can't automatically determine which method represents the correct entry point for the assembly. You must use the compiler's /main switch to identify the name of the class that contains the correct entry point for your application.

10 / 10

Vous aimerez peut-être aussi