1. Trang chủ
  2. » Khoa Học Tự Nhiên

2004 ms c sharp programmers cookbook

900 92 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Table of Contents

  • BackCover

  • C# Programmer's Cookbook

  • Introduction

    • Code Samples

    • System Requirements

    • Other Books

    • Microsoft Press Support

  • Chapter 1: Application Development

    • 1.1 Create a Console Application

    • 1.2 Create a Windows-Based Application

    • 1.3 Create and Use a Code Module

    • 1.4 Create and Use a Code Library

    • 1.5 Access Command-Line Arguments

    • 1.6 Selectively Include Code at Build Time

    • 1.7 Access a Program Element That Has the Same Name as a Keyword

    • 1.8 Create and Manage Strong-Named Key Pairs

    • 1.9 Give an Assembly a Strong Name

    • 1.10 Verify That a Strong-Named Assembly Has Not Been Modified

    • 1.11 Delay Sign an Assembly

    • 1.12 Sign an Assembly with an Authenticode Digital Signature

    • 1.13 Create and Trust a Test Software Publisher Certificate

    • 1.14 Manage the Global Assembly Cache

    • 1.15 Prevent People from Decompiling Your Code

  • Chapter 2: Working with Data

    • 2.2 Encode a String Using Alternate Character Encoding

    • 2.3 Convert Basic Value Types to Byte Arrays

    • 2.4 Encode Binary Data as Text

    • 2.5 Validate Input Using Regular Expressions

    • 2.6 Use Compiled Regular Expressions

    • 2.7 Create Dates and Times from Strings

    • 2.8 Add, Subtract, and Compare Dates and Times

    • 2.9 Sort an Array or an ArrayList

    • 2.10 Copy a Collection to an Array

    • 2.11 Create a Strongly Typed Collection

    • 2.12 Store a Serializable Object to a File

  • Chapter 3: Application Domains, Reflection, and Metadata

    • 3.2 Pass Objects Across Application Domain Boundaries

    • 3.3 Avoid Loading Unnecessary Assemblies into Application Domains

    • 3.4 Create a Type That Can't Cross Application Domain Boundaries

    • 3.5 Load an Assembly into the Current Application Domain

    • 3.6 Execute an Assembly in a Different Application Domain

    • 3.7 Instantiate a Type in a Different Application Domain

    • 3.8 Pass Data Between Application Domains

    • 3.9 Unload Assemblies and Application Domains

    • 3.10 Retrieve Type Information

    • 3.11 Test an Object's Type

    • 3.12 Instantiate an Object Using Reflection

    • 3.13 Create a Custom Attribute

    • 3.14 Inspect the Attributes of a Program Element Using Reflection

  • Chapter 4: Threads, Processes, and Synchronization

    • 4.2 Execute a Method Asynchronously

    • 4.3 Execute a Method Using a Timer

    • 4.4 Execute a Method by Signaling a WaitHandle Object

    • 4.5 Execute a Method Using a New Thread

    • 4.6 Control the Execution of a Thread

    • 4.7 Know When a Thread Finishes

    • 4.8 Synchronize the Execution of Multiple Threads

    • 4.9 Create a Thread-Safe Collection Instance

    • 4.10 Start a New Process

    • 4.11 Terminate a Process

    • 4.12 Ensure That Only One Instance of an Application Can Execute Concurrently

  • Chapter 5: XML Processing

    • 5.2 Insert Nodes in an XML Document

    • 5.3 Quickly Append Nodes in an XML Document

    • 5.4 Find Specific Elements by Name

    • 5.5 Get XML Nodes in a Specific XML Namespace

    • 5.6 Find Elements with an XPath Search

    • 5.7 Read and Write XML Without Loading an Entire Document into Memory

    • 5.8 Validate an XML Document Against a Schema

    • 5.9 Use XML Serialization with Custom Objects

    • 5.10 Create a Schema for a .NET Class

    • 5.11 Generate a Class from a Schema

    • 5.12 Perform an XSL Transform

  • Chapter 6: Windows Forms

    • 6.1 Add a Control Programmatically

    • 6.2 Link Data to a Control

    • 6.3 Process All the Controls on a Form

    • 6.4 Track the Visible Forms in an Application

    • 6.5 Find All MDI Child Forms

    • 6.6 Save the Size and Location of a Form

    • 6.7 Force a List Box to Scroll

    • 6.8 Restrict a Text Box to Numeric Input

    • 6.9 Use an Autocomplete Combo Box

    • 6.10 Sort a List View by Any Column

    • 6.11 Link a Context Menu to a Control

    • 6.12 Use Part of a Main Menu for a Context Menu

    • 6.13 Make a Multilingual Form

    • 6.14 Create a Form That Can't Be Moved

    • 6.15 Make a Borderless Form Movable

    • 6.16 Create an Animated System Tray Icon

    • 6.17 Validate an Input Control

    • 6.18 Use a Drag-and-Drop Operation

    • 6.19 Use Context-Sensitive Help

    • 6.20 Apply Windows XP Control Styles

  • Chapter 7: ASP.NET and Web Forms

    • 7.1 Redirect the User to Another Page

    • 7.2 Store Information Between Requests

    • 7.3 Create Stateful Page Member Variables

    • 7.4 Respond to Client-Side Events with JavaScript

    • 7.5 Show a Pop-Up Window with JavaScript

    • 7.6 Programmatically Set Control Focus

    • 7.7 Allow the User to Upload a File

    • 7.8 Use IIS Authentication

    • 7.9 Use Forms Authentication

    • 7.10 Perform Selective Input Validation

    • 7.11 Add Controls to a Web Form Dynamically

    • 7.12 Dynamically Render an Image

    • 7.13 Load User Controls Programmatically

    • 7.14 Use Page and Fragment Caching

    • 7.15 Reuse Data with the ASP.NET Cache

    • 7.16 Enable Web Site Debugging

    • 7.17 Change the Permissions Given to ASP.NET Code

  • Chapter 8: Graphics, Multimedia, and Printing

    • 8.1 Find All Installed Fonts

    • 8.2 Perform Hit Testing with Shapes

    • 8.3 Create an Irregularly Shaped Control

    • 8.4 Create a Movable Sprite

    • 8.5 Create a Scrollable Image

    • 8.6 Perform a Screen Capture

    • 8.7 Use Double Buffering to Increase Redraw Speed

    • 8.8 Show a Thumbnail for an Image

    • 8.9 Play a Simple Beep

    • 8.10 Play a WAV or an MP3 File

    • 8.11 Show an Animation with DirectShow

    • 8.12 Retrieve Information About the Installed Printers

    • 8.13 Print a Simple Document

    • 8.14 Print a Multipage Document

    • 8.15 Print Wrapped Text

    • 8.16 Show a Dynamic Print Preview

    • 8.17 Manage Print Jobs

  • Chapter 9: Files, Directories, and I/O

    • 9.1 Retrieving Information About a File or Directory

    • 9.2 Set File and Directory Attributes

    • 9.3 Copy, Move, or Delete a File or a Directory

    • 9.4 Calculate the Size of a Directory

    • 9.5 Retrieve Version Information for a File

    • 9.6 Show a Just-in-Time Directory Tree in the TreeView Control

    • 9.7 Read and Write a Text File

    • 9.8 Read and Write a Binary File

    • 9.9 Read a File Asynchronously

    • 9.10 Find Files That Match a Wildcard Expression

    • 9.11 Test Two Files for Equality

    • 9.12 Manipulate Strings Representing file names

    • 9.13 Determine if a Path Is a Directory or a File

    • 9.14 Work with Relative Paths

    • 9.15 Create a Temporary File

    • 9.16 Get the Total Free Space on a Drive

    • 9.17 Show the Common File Dialogs

    • 9.18 Use an Isolated Store

    • 9.19 Monitor the File System for Changes

    • 9.20 Access a COM Port

  • Chapter 10: Database Access

    • 10.1 Connect to a Database

    • 10.2 Use Connection Pooling

    • 10.3 Execute a SQL Command or Stored Procedure

    • 10.4 Use Parameters in a SQL Command or Stored Procedure

    • 10.5 Process the Results of a SQL Query Using a Data Reader

    • 10.6 Obtain an XML Document from a SQL Server Query

    • 10.7 Discover All Instances of SQL Server 2000 on Your Network

  • Chapter 11: Networking and Internetworking

    • 11.1 Download a File over HTTP

    • 11.2 Download a File and Process It Using a Stream

    • 11.3 Get an HTML Page from a Site That Requires Authentication

    • 11.4 Display a Web Page in a Windows-Based Application

    • 11.5 Get the IP Address of the Current Computer

    • 11.6 Resolve a Host Name to an IP Address

    • 11.7 Ping an IP Address

    • 11.8 Communicate Using TCP

    • 11.9 Get the Client IP Address from a Socket Connection

    • 11.10 Set Socket Options

    • 11.11 Create a Multithreaded TCP Server

    • 11.12 Use TCP Asynchronously

    • 11.13 Communicate Using UDP

    • 11.14 Send E-Mail Through SMTP

    • 11.15 Send and Retrieve E-Mail with MAPI

  • Chapter 12: XML Web Services and Remoting

    • 12.1 Avoid Hard-Coding the XML Web Service URL

    • 12.2 Use Response Caching in an XML Web Service

    • 12.3 Use Data Caching in an XML Web Service

    • 12.4 Create a Transactional Web Method

    • 12.5 Set Authentication Credentials for an XML Web Service

    • 12.6 Call a Web Method Asynchronously

    • 12.7 Make an Object Remotable

    • 12.8 Register All the Remotable Classes in an Assembly

    • 12.9 Host a Remote Object in IIS

    • 12.10 Fire an Event Over a Remoting Channel

    • 12.11 Control the Lifetime of a Remote Object

    • 12.12 Control Versioning for Remote Objects

    • 12.13 Create a One-Way Method with XML Web Services or Remoting

  • Chapter 13: Runtime Security

    • 13.1 Allow Partially Trusted Code to Use Your Strong-Named Assembly

    • 13.2 Disable Code Access Security

    • 13.3 Disable Execution Permission Checks

    • 13.4 Ensure the Runtime Grants Specific Permissions to Your Assembly

    • 13.5 Limit the Permissions Granted to Your Assembly

    • 13.6 View the Permission Requests Made by an Assembly

    • 13.7 Determine at Run Time if Your Code Has a Specific Permission

    • 13.8 Restrict Who Can Extend Your Classes and Override Class Members

    • 13.9 Inspect an Assembly's Evidence

    • 13.10 Manipulate Evidence as You Load an Assembly

    • 13.11 Manipulate Runtime Security Using Application Domain Evidence

    • 13.12 Manipulate Runtime Security Using Application Domain Security Policy

    • 13.13 Determine if the Current User Is a Member of a Specific Windows Group

    • 13.14 Restrict Which Users Can Execute Your Code

    • 13.15 Impersonate a Windows User

  • Chapter 14: Cryptography

    • 14.1 Create a Cryptographically Random Number

    • 14.2 Calculate the Hash Code of a Password

    • 14.3 Calculate the Hash Code of a File

    • 14.4 Verify a Hash Code

    • 14.5 Ensure Data Integrity Using a Keyed Hash Code

    • 14.6 Protect a File Using Symmetric Encryption

    • 14.7 Derive a Symmetric Encryption Key from a Password

    • 14.8 Send a Secret Securely Using Asymmetric Encryption

    • 14.9 Store an Asymmetric Encryption Key Securely

    • 14.10 Exchange Symmetric Session Keys Securely

  • Chapter 15: Unmanaged Code Interoperability

    • 15.1 Call a Function in an Unmanaged DLL

    • 15.2 Get the Handle for a Control, a Window, or a File

    • 15.3 Call an Unmanaged Function That Uses a Structure

    • 15.4 Call an Unmanaged Function That Uses a Callback

    • 15.5 Retrieve Unmanaged Error Information

    • 15.6 Use a COM Component in a .NET Client

    • 15.7 Release a COM Component Quickly

    • 15.8 Use Optional Parameters

    • 15.9 Use an ActiveX Control in a .NET Client

    • 15.10 Expose a .NET Component Through COM

  • Chapter 16: Commonly Used Interfaces and Patterns

    • 16.2 Implement a Cloneable Type

    • 16.3 Implement a Comparable Type

    • 16.4 Implement an Enumerable Type

    • 16.5 Implement a Disposable Class

    • 16.6 Implement a Formattable Type

    • 16.7 Implement a Custom Exception Class

    • 16.8 Implement a Custom Event Argument

    • 16.9 Implement the Singleton Pattern

    • 16.10 Implement the Observer Pattern

  • Chapter 17: Windows Integration

    • 17.2 Retrieve the Value of an Environment Variable

    • 17.3 Write an Event to the Windows Event Log

    • 17.4 Access the Windows Registry

    • 17.5 Create a Windows Service

    • 17.6 Create a Windows Service Installer

    • 17.7 Create a Shortcut on the Desktop or Start Menu

  • Index

    • Index_A

    • Index_B

    • Index_C

    • Index_D

    • Index_E

    • Index_F

    • Index_G

    • Index_H-I

    • Index_J-K

    • Index_L-M

    • Index_N-O

    • Index_P

    • Index_R

    • Index_S

    • Index_T

    • Index_V

    • Index_W

  • List of Figures

  • List of Tables

Nội dung

C# Programmer's Cookbook ISBN:0735619301 by Allen Jones 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 Application Domains, Reflection, and Chapter 3 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 Back Cover Next time you hit the wall with a tough C# development problem, get the code behind the solution —and solve it the right way The C# Programmer’s Cookbook provides at-a-glance reference to hundreds of C# and Microsoft NET Framework programming scenarios using a concise, problem/solution format The book’s organized so you can quickly zero in on the topics and answers you need—with 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 type—from 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 COMbased 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 ecommerce and security systems 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 13year 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 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 solution—or 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 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 Chapter 12: XML Web Services and Remoting Figure 12.1: Configuring a dynamic XML Web service URL Figure 12.2: Monitoring a failed transaction Figure 12.3: Using a remotable class Chapter 17: Windows Integration Figure 17.1: The Windows Service Start Failure message box List of Tables Chapter 1: Application Development Table 1.1: Logical Operators Supported by the #if #endif Directive Table 1.2: Commonly Used Switches of the File Signing Tool Table 1.3: Commonly Used Switches of the Certificate Creation Tool Chapter 2: Working with Data Table 2.1: Character Encoding Classes Table 2.2: Commonly Used Regular Expression Metacharacter Elements Table 2.3: Commonly Used Regular Expressions Table 2.4: Operators Supported by the DateTime and TimeSpan Chapter 3: Application Domains, Reflection, and Metadata Table 3.1: Commonly Used AppDomainSetup Properties Table 3.2: Methods That Return Type Objects Table 3.3: Members of the AttributeUsage Enumeration Chapter 4: Threads, Processes, and Synchronization Table 4.1: Controlling the Execution of a Thread Table 4.2: WaitHandle Methods for Synchronizing Thread Execution Table 4.3: Properties of the ProcessStartInfo Class Table 4.4: Methods for Obtaining Process References Chapter 5: XML Processing Table 5.1: XPath Expression Syntax Chapter 7: ASP.NET and Web Forms Table 7.1: Types of State Management Table 7.2: Types of Authentication Chapter 9: Files, Directories, and I/O Table 9.1: Members for Files and Directories Table 9.2: Methods for Manipulating a FileInfo Object Table 9.3: Methods for Manipulating a DirectoryInfo Object Chapter 10: Database Access Table 10.1: NET Framework Data Provider Implementations Table 10.2: Connection String Settings That Control Connection Pooling Table 10.3: Common Command Object Properties Table 10.4: Parameter Properties Table 10.5: Commonly Used Members of Data Reader Classes Chapter 13: Runtime Security Table 13.1: Evidence Classes That Generate Identity Permissions Table 13.2: Classes and Their Methods That Allow You to Assign Evidence to an Assembly Table 13.3: Windows Built-In Account Names and Identifiers Table 13.4: Members of the PrincipalPolicy Enumeration Chapter 14: Cryptography Table 14.1: Hashing Algorithm Implementations Table 14.2: Keyed Hashing Algorithm Implementations Table 14.3: Symmetric Algorithm Implementations Table 14.4: Operation of a CryptoStream Object Chapter 16: Commonly Used Interfaces and Patterns Table 16.1: Members of the IEnumerator Interface Chapter 17: Windows Integration Table 17.1: Commonly Used Members of the Environment Class Table 17.2: Determining the Current Operating System Table 17.3: Static Fields of the Registry Class Table 17.4: RegistryKey Methods to Create, Read, Update, and Delete Registry Keys and Values Table 17.5: Methods That Control the Operation of a Service ... 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... WriteString("Welcome to the C# Programmer's Cookbook, " } } 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... However, the C# compiler can't compile your Microsoft Visual Basic NET or COBOL NET code for inclusion in your assembly You must first use a languagespecific compiler to turn your source into MSIL in a structure that

Ngày đăng: 25/03/2019, 17:12

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN