Visual C# 2010 Recipes - A Problem-Solution Approach

von: Allen Jones, Matthew MacDonald, Rakesh Rajan, Adam Freeman

Apress, 2010

ISBN: 9781430225263 , 1016 Seiten

Format: PDF, OL

Kopierschutz: Wasserzeichen

Windows PC,Mac OSX geeignet für alle DRM-fähigen eReader Apple iPad, Android Tablet PC's Online-Lesen für: Windows PC,Mac OSX,Linux

Preis: 66,99 EUR

Mehr zum Inhalt

Visual C# 2010 Recipes - A Problem-Solution Approach


 

Contents at a Glance

5

Table of Contents

7

About the Authors

21

About the Technical Reviewer

22

Acknowledgments

23

Introduction

24

CHAPTER 1 Application Development

25

1-1. Create a Console Application from the Command Line

26

Problem

26

Solution

26

How It Works

27

The Code

27

Usage

28

1-2. Create a Windows-Based Application from the Command Line

29

Problem

29

Solution

29

How It Works

29

The Code

30

Usage

32

1-3. Create and Use a Code Module

33

Problem

33

Solution

33

How It Works

33

Usage

34

1-4. Create and Use a Code Library from the Command Line

35

Problem

35

Solution

35

How It Works

35

Usage

35

1-5. Access Command-Line Arguments

36

Problem

36

Solution

36

How It Works

36

The Code

37

Usage

37

1-6. Include Code Selectively at Build Time

38

Problem

38

Solution

38

How It Works

39

The Code

40

Usage

41

Notes

41

1-7. Access a Program Element That Has the Same Name As a Keyword

42

Problem

42

Solution

42

How It Works

42

The Code

42

1-8. Create and Manage Strongly Named Key Pairs

43

Problem

43

Solution

43

How It Works

43

1-9. Give an Assembly a Strong Name

45

Problem

45

Solution

45

How It Works

46

The Code

46

Usage

47

1-10. Verify That a Strongly Named Assembly Has Not Been Modified

47

Problem

47

Solution

47

How It Works

47

1-11. Delay-Sign an Assembly

48

Problem

48

Solution

49

How It Works

49

1-12. Sign an Assembly with an Authenticode Digital Signature

50

Problem

50

Solution

50

How It Works

51

Usage

51

1-13. Create and Trust a Test Software Publisher Certificate

52

Problem

52

Solution

52

How It Works

52

1-14. Manage the Global Assembly Cache

53

Problem

53

Solution

54

How It Works

54

1-15. Prevent People from Decompiling Your Code

54

Problem

54

Solution

54

How It Works

55

1-16. Manipulate the Appearance of the Console

55

Problem

55

Solution

56

How It Works

56

The Code

57

1-17. Create a Static Class

58

Problem

58

Solution

58

How It Works

58

The Code

58

1-18. Create an Anonymous Type

59

Problem

59

Solution

59

How It Works

59

The Code

60

1-19. Create an ExpandoObject Dynamic Type

61

Problem

61

Solution

61

How It Works

61

The Code

62

1-20. Define an Automatically Implemented Property

63

Problem

63

Solution

63

How It Works

63

The Code

63

1-21. Overload an Operator

65

Problem

65

Solution

65

How It Works

65

The Code

66

1-22. Define a Conversion Operator

68

Problem

68

Solution

68

How It Works

68

The Code

69

1-23. Handle an Event with an Anonymous Function

70

Problem

70

Solution

70

How It Works

70

The Code

71

1-24. Implement a Custom Indexer

72

Problem

72

Solution

72

How It Works

72

The Code

72

CHAPTER 2 Data Manipulation

76

2-1. Manipulate the Contents of a String Efficiently

77

Problem

77

Solution

77

How It Works

77

The Code

78

2-2. Encode a String Using Alternate Character Encoding

79

Problem

79

Solution

79

How It Works

79

The Code

80

Usage

81

2-3. Convert Basic Value Types to Byte Arrays

82

Problem

82

Solution

82

How It Works

82

The Code

83

2-4. Base64 Encode Binary Data

84

Problem

84

Solution

85

How It Works

85

The Code

85

2-5. Validate Input Using Regular Expressions

88

Problem

88

Solution

89

How It Works

89

The Code

91

Usage

92

Notes

92

2-6. Use Compiled Regular Expressions

93

Problem

93

Solution

93

How It Works

93

The Code

94

2-7. Create Dates and Times from Strings

95

Problem

95

Solution

95

How It Works

96

The Code

96

2-8. Add, Subtract, and Compare Dates and Times

97

Problem

97

Solution

98

How It Works

98

The Code

99

2-9. Sort an Array or a Collection

100

Problem

100

Solution

100

How It Works

100

The Code

101

2-10. Copy a Collection to an Array

102

Problem

102

Solution

102

How It Works

103

The Code

103

2-11. Use a Strongly Typed Collection

104

Problem

104

Solution

104

How It Works

105

The Code

105

2-12. Create a Generic Type

106

Problem

106

Solution

106

How It Works

106

The Code

107

2-13. Store a Serializable Object to a File

110

Problem

110

Solution

110

How It Works

110

The Code

110

2-14. Serialize an Object Using JSON

113

Problem

113

Solution

114

How It Works

114

The Code

115

2-15. Read User Input from the Console

116

Problem

116

Solution

116

How It Works

116

The Code

117

2-16. Using Large Integer Values

119

Problem

119

Solution

119

How It Works

119

The Code

119

2-17. Select Collection or Array Elements

120

Problem

120

Solution

120

How It Works

120

The Code

121

2-18. Remove Duplicate Items from an Array or Collection

123

Problem

123

Solution

123

How It Works

123

The Code

123

CHAPTER 3 Application Domains, Reflection, and Metadata

125

3-1. Create an Application Domain

126

Problem

126

Solution

126

How It Works

126

The Code

127

3-2. Create Types That Can Be Passed Across Application Domain Boundaries

128

Problem

128

Solution

128

How It Works

128

The Code

129

3-3. Avoid Loading Unnecessary Assemblies into Application Domains

131

Problem

131

Solution

131

How It Works

131

The Code

132

3-4. Create a Type That Cannot Cross Application Domain Boundaries

133

Problem

133

Solution

133

How It Works

133

3-5. Load an Assembly into the Current Application Domain

133

Problem

133

Solution

133

How It Works

134

The Code

135

3-6. Execute an Assembly in a Different Application Domain

136

Problem

136

Solution

136

How It Works

136

The Code

137

Usage

138

3-7. Instantiate a Type in a Different Application Domain

138

Problem

138

Solution

138

How It Works

138

The Code

139

3-8. Pass Data Between Application Domains

143

Problem

143

Solution

143

How It Works

143

The Code

144

3-9. Unload Assemblies and Application Domains

146

Problem

146

Solution

146

How It Works

146

The Code

146

3-10. Retrieve Type Information

147

Problem

147

Solution

147

How It Works

147

The Code

148

3-11. Test an Object’s Type

149

Problem

149

Solution

149

How It Works

149

The Code

150

3-12. Instantiate an Object Using Reflection

151

Problem

151

Solution

152

How It Works

152

The Code

152

3-13. Create a Custom Attribute

155

Problem

155

Solution

155

How It Works

155

The Code

156

Usage

157

3-14. Inspect the Attributes of a Program Element Using Reflection

158

Problem

158

Solution

158

How It Works

158

The Code

159

3-15. Programmatically Discover the Members of a Type

159

Problem

159

Solution

160

How It Works

160

The Code

160

3-16. Invoke a Type Member Using Reflection

162

Problem

162

Solution

162

How It Works

162

The Code

162

3-17. Dynamically Invoke a Type Member

164

Problem

164

Solution

164

How It Works

164

The Code

164

3-18. Create a Custom Dynamic Type

165

Problem

165

Solution

165

How It Works

165

The Code

167

CHAPTER 4 Threads, Processes, and Synchronization

170

4-1. Execute a Method Using the Thread Pool

172

Problem

172

Solution

172

How It Works

172

The Code

172

Notes

174

4-2. Execute a Method Asynchronously

175

Problem

175

Solution

175

How It Works

176

The Code

177

4-3. Execute a Method Periodically

184

Problem

184

Solution

185

How It Works

185

The Code

185

4-4. Execute a Method at a Specific Time

187

Problem

187

Solution

187

How It Works

187

The Code

187

4-5. Execute a Method by Signaling a WaitHandle Object

188

Problem

188

Solution

188

How It Works

188

The Code

189

4-6. Execute a Method Using a New Thread

190

Problem

190

Solution

190

How It Works

190

The Code

191

4-7. Synchronize the Execution of Multiple Threads Using a Monitor

193

Problem

193

Solution

193

How It Works

193

The Code

195

4-8. Synchronize the Execution of Multiple Threads Using an Event

199

Problem

199

Solution

199

How It Works

199

The Code

200

4-9. Synchronize the Execution of Multiple Threads Using a Mutex

203

Problem

203

Solution

203

How It Works

203

The Code

203

4-10. Synchronize the Execution of Multiple Threads Using a Semaphore

206

Problem

206

Solution

206

How It Works

206

The Code

206

4-11. Synchronize Access to a Shared Data Value

208

Problem

208

Solution

208

How It Works

208

The Code

209

4-12. Know When a Thread Finishes

211

Problem

211

Solution

211

How It Works

211

The Code

211

4-13. Terminate the Execution of a Thread

212

Problem

212

Solution

213

How It Works

213

The Code

213

4-14. Create a Thread-Safe Collection Instance

214

Problem

214

Solution

215

How It Works

215

The Code

216

4-15. Start a New Process

216

Problem

216

Solution

217

How It Works

217

The Code

218

4-16. Terminate a Process

220

Problem

220

Solution

220

How It Works

220

The Code

221

4-17. Ensure That Only One Instance of an Application Can Execute Concurrently

222

Problem

222

Solution

222

How It Works

222

The Code

223

CHAPTER 5 Files, Directories, and I/O

225

5-1. Retrieve Information About a File, Directory, or Drive

226

Problem

226

Solution

226

How It Works

226

The Code

228

Usage

230

5-2. Set File and Directory Attributes

231

Problem

231

Solution

231

How It Works

231

The Code

232

5-3. Copy, Move, or Delete a File or Directory

233

Problem

233

Solution

233

How It Works

233

The Code

234

5-4. Calculate the Size of a Directory

236

Problem

236

Solution

236

How It Works

236

The Code

236

5-5. Retrieve Version Information for a File

237

Problem

237

Solution

237

How It Works

237

The Code

238

Usage

238

5-6. Show a Just-in-Time Directory Tree in the TreeView Control

239

Problem

239

Solution

239

How It Works

240

The Code

240

5-7. Read and Write a Text File

242

Problem

242

Solution

242

How It Works

242

The Code

243

5-8. Read and Write a Binary File

244

Problem

244

Solution

245

How It Works

245

The Code

245

5-9. Read a File Asynchronously

246

Problem

246

Solution

246

How It Works

247

The Code

247

Usage

248

5-10. Find Files That Match a Wildcard Expression

250

Problem

250

Solution

250

How It Works

250

The Code

250

5-11. Test Two Files for Equality

251

Problem

251

Solution

251

How It Works

252

The Code

252

5-12. Manipulate Strings Representing File Names

253

Problem

253

Solution

253

How It Works

253

5-13. Determine If a Path Is a Directory or a File

255

Problem

255

Solution

255

How It Works

255

The Code

255

5-14. Work with Relative Paths

256

Problem

256

Solution

256

How It Works

256

The Code

256

Usage

257

5-15. Create a Temporary File

258

Problem

258

Solution

258

How It Works

258

The Code

258

5-16. Get the Total Free Space on a Drive

259

Problem

259

Solution

259

How It Works

259

The Code

259

5-17. Show the Common File Dialog Boxes

261

Problem

261

Solution

261

How It Works

261

The Code

262

5-18. Use an Isolated Store

263

Problem

263

Solution

264

How It Works

264

The Code

264

5-19. Monitor the File System for Changes

266

Problem

266

Solution

266

How It Works

266

The Code

267

5-20. Access a COM Port

268

Problem

268

Solution

268

How It Works

268

The Code

268

5-21. Get a Random File Name

269

Problem

269

Solution

269

How It Works

269

The Code

270

5-22. Manipulate the Access Control List of a File or Directory

270

Problem

270

Solution

270

How It Works

271

The Code

271

5-23. Compress Data

273

Problem

273

Solution

273

How It Works

273

The Code

273

5-24. Log Data to a File

274

Problem

274

Solution

274

How It Works

275

The Code

275

5-25. Process a Log File

276

Problem

276

Solution

276

How It Works

276

The Code

276

5-26. Communicate Between Processes

277

Problem

277

Solution

277

How It Works

277

The Code

277

CHAPTER 6 XML Processing

280

6-1. Show the Structure of an XML Document in a TreeView

280

Problem

280

Solution

281

How It Works

281

The Code

282

Usage

284

6-2. Insert Nodes in an XML Document

285

Problem

285

Solution

285

How It Works

285

The Code

286

6-3. Quickly Append Nodes in an XML Document

287

Problem

287

Solution

288

How It Works

288

The Code

288

6-4. Find Specific Elements by Name

290

Problem

290

Solution

290

How It Works

290

The Code

290

6-5. Get XML Nodes in a Specific XML Namespace

291

Problem

291

Solution

292

How It Works

292

The Code

292

6-6. Find Elements with an XPath Search

293

Problem

293

Solution

294

How It Works

294

The Code

294

Notes

295

6-7. Read and Write XML Without Loading an Entire Document into Memory

297

Problem

297

Solution

297

How It Works

297

The Code

298

6-8. Validate an XML Document Against a Schema

300

Problem

300

Solution

300

How It Works

300

The Code

302

6-9. Use XML Serialization with Custom Objects

305

Problem

305

Solution

305

How It Works

306

The Code

307

6-10. Create a Schema for a .NET Class

309

Problem

309

Solution

309

How It Works

309

Usage

309

6-11. Generate a Class from a Schema

310

Problem

310

Solution

310

How It Works

310

Usage

310

6-12. Perform an XSL Transform

311

Problem

311

Solution

311

How It Works

311

The Code

311

6-13. Load XML with LINQ

315

Problem

315

Solution

315

How It Works

315

The Code

315

6-14. Create a New XML Tree with LINQ

317

Problem

317

Solution

317

How It Works

317

The Code

318

6-15. Query XML with LINQ

319

Problem

319

Solution

320

How It Works

320

The Code

320

6-16. Modify an XML Tree with LINQ

322

The Problem

322

The Solution

322

How It Works

322

The Code

322

CHAPTER 7 Windows Forms

325

7-1. Add a Control Programmatically

327

Problem

327

Solution

327

How It Works

327

The Code

327

7-2. Store Data with a Control

329

Problem

329

Solution

329

How It Works

330

The Code

330

7-3. Process All the Controls on a Form

332

Problem

332

Solution

332

How It Works

332

The Code

332

7-4. Track the Visible Forms in an Application

333

Problem

333

Solution

333

How It Works

333

The Code

334

Notes

336

7-5. Find All MDI Child Forms

337

Problem

337

Solution

337

How It Works

337

The Code

337

7-6. Save Configuration Settings for a Form

340

Problem

340

Solution

340

How It Works

340

The Code

341

7-7. Force a List Box to Scroll to the Most Recently Added Item

343

Problem

343

Solution

343

How It Works

343

The Code

343

7-8. Restrict a Text Box to Accept Only Specific Input

344

Problem

344

Solution

344

How It Works

344

The Code

345

Notes

347

7-9. Use an Autocomplete Combo Box or Text Box

347

Problem

347

Solution

347

How It Works

347

The Code

348

7-10. Sort a List View by Any Column

350

Problem

350

Solution

350

How It Works

350

The Code

350

7-11. Lay Out Controls Automatically

353

Problem

353

Solution

353

How It Works

353

7-12. Use Part of a Main Menu for a Context Menu

354

Problem

354

Solution

354

How It Works

354

The Code

354

7-13. Make a Multilingual Form

356

Problem

356

Solution

356

How It Works

356

The Code

358

7-14. Create a Form That Cannot Be Moved

359

Problem

359

Solution

359

How It Works

359

The Code

360

7-15. Make a Borderless Form Movable

361

Problem

361

Solution

361

How It Works

361

The Code

362

7-16. Create an Animated System Tray Icon

364

Problem

364

Solution

364

How It Works

364

The Code

364

7-17. Validate an Input Control

365

Problem

365

Solution

366

How It Works

366

The Code

366

7-18. Use a Drag-and-Drop Operation

368

Problem

368

Solution

368

How It Works

369

The Code

369

7-19. Update the User Interface in a Multithreaded Application

372

Problem

372

Solution

372

How It Works

372

The Code

373

7-20. Display a Web Page in a Windows-Based Application

374

Problem

374

Solution

374

How It Works

375

The Code

376

7-21. Display WPF Windows in a Windows Forms Application

378

Problem

378

Solution

378

How It Works

378

The Code

378

7-22. Display WPF Controls in Windows Forms

381

Problem

381

Solution

381

How It Works

381

The Code

382

CHAPTER 8 Graphics, Multimedia, and Printing

386

8-1. Find All Installed Fonts

387

Problem

387

Solution

387

How It Works

387

The Code

387

8-2. Perform Hit Testing with Shapes

389

Problem

389

Solution

389

How It Works

389

The Code

390

8-3. Create an Irregularly Shaped Control

393

Problem

393

Solution

393

How It Works

393

The Code

394

8-4. Create a Movable Sprite

396

Problem

396

Solution

396

How It Works

396

The Code

396

8-5. Create a Scrollable Image

400

Problem

400

Solution

400

How It Works

400

The Code

400

8-6. Perform a Screen Capture

402

Problem

402

Solution

402

How It Works

402

The Code

402

8-7. Use Double Buffering to Increase Redraw Speed

403

Problem

403

Solution

403

How It Works

404

The Code

404

8-8. Show a Thumbnail for an Image

406

Problem

406

Solution

406

How It Works

406

The Code

406

8-9. Play a Simple Beep or System Sound

408

Problem

408

Solution

408

How It Works

408

The Code

408

8-10. Play a WAV File

409

Problem

409

Solution

409

How It Works

409

The Code

409

8-11. Play a Sound File

410

Problem

410

Solution

411

How It Works

411

The Code

411

8-12. Play a Video

412

Problem

412

Solution

412

How It Works

412

The Code

413

8-13. Retrieve Information About Installed Printers

415

Problem

415

Solution

415

How It Works

415

The Code

416

Usage

417

8-14. Print a Simple Document

418

Problem

418

Solution

418

How It Works

418

The Code

420

8-15. Print a Multipage Document

421

Problem

421

Solution

421

How It Works

422

The Code

422

8-16. Print Wrapped Text

425

Problem

425

Solution

425

How It Works

425

The Code

425

8-17. Show a Dynamic Print Preview

427

Problem

427

Solution

427

How It Works

427

The Code

428

8-18. Manage Print Jobs

429

Problem

429

Solution

429

How It Works

430

The Code

430

8-19. Perform Text-to-Speech

434

Problem

434

Solution

434

How It Works

435

The Code

435

8-20. Recognize Characters in an Image (OCR)

436

Problem

436

Solution

437

How It Works

437

The Code

438

CHAPTER 9 Database Access

440

9-1. Connect to a Database

442

Problem

442

Solution

442

How It Works

442

The Code

443

9-2. Use Connection Pooling

445

Problem

445

Solution

445

How It Works

445

The Code

446

Notes

448

9-3. Create a Database Connection String Programmatically

448

Problem

448

Solution

448

How It Works

448

The Code

449

9-4. Store a Database Connection String Securely

450

Problem

450

Solution

450

How It Works

450

The Code

451

9-5. Execute a SQL Command or Stored Procedure

453

Problem

453

Solution

454

How It Works

454

The Code

455

9-6. Use Parameters in a SQL Command or Stored Procedure

458

Problem

458

Solution

458

How It Works

458

The Code

459

9-7. Process the Results of a SQL Query Using a Data Reader

462

Problem

462

Solution

462

How It Works

462

The Code

464

9-8. Obtain an XML Document from a SQL Server Query

465

Problem

465

Solution

465

How It Works

466

The Code

467

9-9. Perform Asynchronous Database Operations Against SQL Server

469

Problem

469

Solution

469

How It Works

469

The Code

471

9-10. Write Database-Independent Code

473

Problem

473

Solution

473

How It Works

473

The Code

476

9-11. Discover All Instances of SQL Server on Your Network

477

Problem

477

Solution

477

How It Works

478

The Code

478

9-12. Create an In-Memory Cache

479

Problem

479

Solution

479

How It Works

479

The Code

480

9-13. Create a DataSet Programmatically

483

Problem

483

Solution

483

How It Works

483

The Code

483

9-14. Perform a LINQ Query

485

Problem

485

Solution

486

How It Works

486

The Code

486

9-15. Perform a LINQ Query with Entity Types

488

Problem

488

Solution

488

How It Works

488

The Code

489

9-16. Compare LINQ DataSet Results

490

Problem

490

Solution

490

How It Works

491

The Code

491

CHAPTER 10 Networking

495

10-1. Obtain Information About the Local Network Interface

496

Problem

496

Solution

496

How It Works

496

The Code

498

10-2. Detect Changes in Network Connectivity

500

Problem

500

Solution

500

How It Works

500

The Code

500

10-3. Download Data over HTTP or FTP

502

Problem

502

Solution

502

How It Works

502

The Code

503

Notes

505

10-4. Download a File and Process It Using a Stream

506

Problem

506

Solution

506

How It Works

506

The Code

507

10-5. Respond to HTTP Requests from Within Your Application

508

Problem

508

Solution

509

How It Works

509

The Code

510

10-6. Get an HTML Page from a Site That Requires Authentication

513

Problem

513

Solution

513

How It Works

513

The Code

514

10-7. Send E-mail Using SMTP

515

Problem

515

Solution

516

How It Works

516

The Code

518

10-8. Resolve a Host Name to an IP Address

520

Problem

520

Solution

520

How It Works

521

The Code

521

Usage

522

10-9. Ping an IP Address

523

Problem

523

Solution

523

How It Works

523

The Code

524

Usage

525

10-10. Communicate Using TCP

526

Problem

526

Solution

526

How It Works

526

The Code

527

Usage

530

10-11. Create a Multithreaded TCP Server That Supports Asynchronous Communications

531

Problem

531

Solution

531

How It Works

531

The Code

532

10-12. Communicate Using UDP

539

Problem

539

Solution

539

How It Works

540

The Code

540

10-13. Create a SOAP-Based Web Service

542

Problem

542

Solution

542

How It Works

543

The Code

543

10-14. Call a WCF Service Using a Dynamically Generated Service Proxy

548

Problem

548

Solution

549

How It Works

549

The Code

549

10-15. Process the Content of an Atom or RSS Feed

550

Problem

550

Solution

550

How It Works

550

The Code

552

10-16. Manipulate URIs

554

Problem

554

Solution

554

How It Works

554

The Code

556

CHAPTER 11 Security and Cryptography

558

11-1. Allow Partially Trusted Code to Use Your Strongly Named Assembly

559

Problem

559

Solution

559

How It Works

560

The Code

561

11-2. Disable Code Access Security

562

Problem

562

Solution

562

How It Works

562

11-3. Disable Execution Permission Checks

563

Problem

563

Solution

563

How It Works

563

The Code

563

11-4. Ensure the Runtime Grants Specific Permissions to Your Assembly

564

Problem

564

Solution

565

How It Works

565

The Code

566

11-5. Limit the Permissions Granted to Your Assembly

567

Problem

567

Solution

567

How It Works

567

The Code

568

11-6. View the Permissions Required by an Assembly

569

Problem

569

Solution

569

How It Works

569

The Code

570

Usage

571

11-7. Determine at Runtime If Your Code Has a Specific Permission

573

Problem

573

Solution

573

How It Works

573

The Code

574

11-8. Restrict Who Can Extend Your Classes and Override Class Members

574

Problem

574

Solution

575

How It Works

575

The Code

576

11-9. Inspect an Assembly’s Evidence

577

Problem

577

Solution

577

How It Works

577

The Code

577

11-10. Determine If the Current User Is a Member of a Specific Windows Group

579

Problem

579

Solution

579

How It Works

579

The Code

581

Usage

582

11-11. Restrict Which Users Can Execute Your Code

583

Problem

583

Solution

583

How It Works

583

The Code

585

11-12. Impersonate a Windows User

587

Problem

587

Solution

587

How It Works

587

The Code

588

Usage

590

11-13. Create a Cryptographically Random Number

590

Problem

590

Solution

590

How It Works

591

The Code

591

11-14. Calculate the Hash Code of a Password

592

Problem

592

Solution

592

How It Works

593

The Code

594

Usage

595

11-15. Calculate the Hash Code of a File

596

Problem

596

Solution

596

How It Works

596

The Code

597

Usage

597

11-16. Verify a Hash Code

598

Problem

598

Solution

598

How It Works

598

The Code

599

11-17. Ensure Data Integrity Using a Keyed Hash Code

601

Problem

601

Solution

601

How It Works

601

The Code

602

Usage

603

11-18. Work with Security-Sensitive Strings in Memory

604

Problem

604

Solution

604

How It Works

604

The Code

605

11-19. Encrypt and Decrypt Data Using the Data Protection API

607

Problem

607

Solution

607

How It Works

608

The Code

609

CHAPTER 12 Unmanaged Code Interoperability

611

12-1. Call a Function in an Unmanaged DLL

611

Problem

611

Solution

612

How It Works

612

The Code

612

12-2. Get the Handle for a Control, Window, or File

615

Problem

615

Solution

615

How It Works

615

The Code

615

12-3. Call an Unmanaged Function That Uses a Structure

617

Problem

617

Solution

617

How It Works

617

The Code

617

12-4. Call an Unmanaged Function That Uses a Callback

620

Problem

620

Solution

620

How It Works

620

The Code

621

12-5. Retrieve Unmanaged Error Information

622

Problem

622

Solution

622

How It Works

622

The Code

622

12-6. Use a COM Component in a .NET Client

624

Problem

624

Solution

624

How It Works

624

The Code

625

12-7. Release a COM Component Quickly

627

Problem

627

Solution

627

How It Works

627

12-8. Use Optional Parameters

628

Problem

628

Solution

628

How It Works

628

The Code

628

12-9. Use an ActiveX Control in a .NET Client

629

Problem

629

Solution

629

How It Works

629

12-10. Expose a .NET Component Through COM

630

Problem

630

Solution

630

How It Works

630

CHAPTER 13 Commonly Used Interfaces and Patterns

632

13-1. Implement a Custom Serializable Type

633

Problem

633

Solution

633

How It Works

633

The Code

636

13-2. Implement a Cloneable Type

639

Problem

639

Solution

639

How It Works

639

The Code

641

13-3. Implement a Comparable Type

644

Problem

644

Solution

644

How It Works

644

The Code

645

Usage

647

13-4. Implement an Enumerable Collection

649

Problem

649

Solution

649

How It Works

649

The Code

650

13-5. Implement an Enumerable Type Using a Custom Iterator

653

Problem

653

Solution

653

How It Works

653

The Code

654

13-6. Implement a Disposable Class

660

Problem

660

Solution

660

How It Works

660

The Code

662

13-7. Implement a Formattable Type

664

Problem

664

Solution

664

How It Works

664

The Code

665

Usage

668

13-8. Implement a Custom Exception Class

668

Problem

668

Solution

668

How It Works

668

The Code

670

13-9. Implement a Custom Event Argument

672

Problem

672

Solution

672

How It Works

672

The Code

673

13-10. Implement the Singleton Pattern

674

Problem

674

Solution

674

How It Works

674

The Code

675

Usage

675

13-11. Implement the Observer Pattern

676

Problem

676

Solution

676

How It Works

676

The Code

676

Usage

681

13-12. Implement a Parallel Producer-Consumer Pattern

682

Problem

682

Solution

682

How It Works

682

The Code

683

13-13. Perform Lazy Object Initialization

684

Problem

684

Solution

684

How It Works

685

The Code

685

13-14. Use Optional Parameters

686

Problem

686

Solution

686

How It Works

686

The Code

687

13-15. Add a Method to a Type Without Modifying It

688

Problem

688

Solution

688

How It Works

688

The Code

689

13-16. Call an Object Member Dynamically

690

Problem

690

Solution

690

How It Works

690

The Code

690

13-17. Create a Variant Generic Type

692

Problem

692

Solution

692

How It Works

692

The Code

694

CHAPTER 14 Windows Integration

696

14-1. Access Runtime Environment Information

697

Problem

697

Solution

697

How It Works

697

The Code

699

14-2. Retrieve the Value of an Environment Variable

701

Problem

701

Solution

701

How It Works

701

The Code

702

14-3. Write an Event to the Windows Event Log

703

Problem

703

Solution

703

How It Works

703

The Code

704

14-4. Read and Write to the Windows Registry

705

Problem

705

Solution

705

How It Works

705

The Code

706

14-5. Search the Windows Registry

708

Problem

708

Solution

708

How It Works

708

The Code

710

Usage

712

14-6. Create a Windows Service

712

Problem

712

Solution

712

How It Works

712

The Code

714

Usage

717

14-7. Create a Windows Service Installer

717

Problem

717

Solution

717

How It Works

717

The Code

718

Usage

719

14-8. Create a Shortcut on the Desktop or Start Menu

719

Problem

719

Solution

719

How It Works

720

The Code

720

14-9. Create a Windows 7 Jump List

722

Problem

722

Solution

722

How It Works

722

The Code

723

14-10. Use Windows Search

724

Problem

724

Solution

724

How It Works

725

The Code

726

14-11. Check Internet Connectivity

729

Problem

729

Solution

729

How It Works

729

The Code

729

14-12. Display a Task Dialog

730

Problem

730

Solution

730

How It Works

730

The Code

731

14-13. Write Custom Performance Counters

733

Problem

733

Solution

734

How It Works

734

The Code

734

14-14. Read Performance Counters

737

Problem

737

Solution

737

How It Works

737

The Code

738

14-15. Obtain Elevated Privileges

739

Problem

739

Solution

739

How It Works

739

The Code

740

CHAPTER 15 Parallel Programming

742

15-1. Perform Simple Parallel Tasks

742

Problem

742

Solution

742

How It Works

743

The Code

743

15-2. Return a Result from a Task

745

Problem

745

Solution

745

How It Works

745

The Code

745

15-3. Wait for Tasks to Complete

747

Problem

747

Solution

747

How It Works

747

The Code

747

15-4. Parallel Process a Collection

749

Problem

749

Solution

749

How It Works

749

The Code

750

15-5. Chain Tasks Together

751

Problem

751

Solution

751

How It Works

751

The Code

751

15-6. Write a Cooperative Algorithm

752

Problem

752

Solution

752

How It Works

753

The Code

753

15-7. Handle Exceptions in Tasks

754

Problem

754

Solution

755

How It Works

755

The Code

755

15-8. Cancel a Task

756

Problem

756

Solution

756

How It Works

757

The Code

757

15-9. Share Data Between Tasks

758

Problem

758

Solution

758

How It Works

758

The Code

759

CHAPTER 16 Using LINQ

761

16-1. Perform a Simple LINQ Query

761

Problem

761

Solution

762

How It Works

762

The Code

763

16-2. Filter Items from a Data Source

767

Problem

767

Solution

767

How It Works

767

The Code

767

16-3. Filter a Data Source by Type

769

Problem

769

Solution

769

How It Works

769

The Code

770

16-4. Filter Ranges of Elements

771

Problem

771

Solution

771

How It Works

771

The Code

771

16-5. Select Multiple Member Values

772

Problem

772

Solution

772

How It Works

773

The Code

773

16-6. Filter and Select from Multiple Data Sources

774

Problem

774

Solution

774

How It Works

774

The Code

775

16-7. Use Permutations of Data Sources

777

Problem

777

Solution

777

How It Works

777

The Code

778

16-8. Concatenate Data Sources

779

Problem

779

Solution

779

How It Works

780

The Code

780

16-9. Group Result Elements by Attribute

781

Problem

781

Solution

781

How It Works

781

The Code

782

16-10. Sort Query Results

786

Problem

786

Solution

786

How It Works

786

The Code

786

16-11. Compare Data Sources

788

Problem

788

The Solution

788

How It Works

788

The Code

788

16-12. Aggregate Data Sources

790

Problem

790

Solution

790

How It Works

790

The Code

791

16-13. Share Values Within a Query

792

Problem

792

Solution

793

How It Works

793

The Code

793

16-14. Create Custom LINQ Extension Methods

794

Problem

794

Solution

794

How It Works

794

The Code

795

16-15. Convert from IEnumerable<>

796

Problem

796

Solution

796

How It Works

796

The Code

796

CHAPTER 17 Windows Presentation Foundation

801

17-1. Create and Use a Dependency Property

802

Problem

802

Solution

802

How It Works

803

The Code

805

17-2. Create and Use an Attached Property

807

Problem

807

Solution

808

How It Works

808

The Code

808

17-3. Define Application-Wide Resources

811

Problem

811

Solution

811

How It Works

811

The Code

812

17-4. Debug Data Bindings Using an IValueConverter

813

Problem

813

Solution

813

How It Works

813

The Code

813

17-5. Debug Bindings Using Attached Properties

815

Problem

815

Solution

815

How It Works

816

The Code

817

17-6. Arrange UI Elements in a Horizontal or Vertical Stack

817

Problem

817

Solution

817

How It Works

817

The Code

818

17-7. Dock UI Elements to the Edges of a Form

819

Problem

819

Solution

819

How It Works

820

The Code

820

17-8. Arrange UI Elements in a Grid

821

Problem

821

Solution

821

How It Works

822

The Code

822

17-9. Position UI Elements Using Exact Coordinates

823

Problem

823

Solution

824

How It Works

824

The Code

824

17-10. Get Rich Text Input from a User

825

Problem

825

Solution

825

How It Works

825

The Code

827

17-11. Display a Control Rotated

830

Problem

830

Solution

830

How It Works

830

The Code

830

17-12. Create a User Control

832

Problem

832

Solution

832

How It Works

833

The Code

833

17-13. Support Application Commands in a User Control

834

Problem

834

Solution

835

How It Works

835

The Code

835

17-14. Create a Lookless Custom Control

838

Problem

838

Solution

839

How It Works

839

The Code

841

17-15. Create a Two-Way Binding

845

Problem

845

Solution

845

How It Works

845

The Code

847

17-16. Bind to a Command

848

Problem

848

Solution

848

How It Works

848

The Code

849

17-17. Use Data Templates to Display Bound Data

856

Problem

856

Solution

856

How It Works

856

The Code

857

17-18. Bind to a Collection with the Master-Detail Pattern

860

Problem

860

Solution

860

How It Works

860

The Code

861

17-19. Change a Control’s Appearance on Mouseover

866

Problem

866

Solution

866

How It Works

867

The Code

867

17-20. Change the Appearance of Alternate Items in a List

868

Problem

868

Solution

868

How It Works

868

The Code

868

17-21. Drag Items from a List and Drop Them on a Canvas

870

Problem

870

Solution

870

How It Works

870

The Code

871

17-22. Display the Progress of a Long-Running Operation and Allow the User to Cancel It

874

Problem

874

Solution

874

How It Works

875

The Code

875

17-23. Draw Two-Dimensional Shapes

878

Problem

878

Solution

878

How It Works

879

The Code

881

17-24. Create Reusable Shapes

883

Problem

883

Solution

883

How It Works

883

The Code

884

17-25. Draw or Fill a Shape Using a Solid Color

885

Problem

885

Solution

885

How It Works

885

The Code

886

17-26. Fill a Shape with a Linear or Radial Color Gradient

887

Problem

887

Solution

888

How It Works

888

The Code

889

17-27. Fill a Shape with an Image

891

Problem

891

Solution

891

How It Works

891

The Code

893

17-28. Fill a Shape with a Pattern or Texture

894

Problem

894

Solution

894

How It Works

894

The Code

895

17-29. Animate the Property of a Control

898

Problem

898

Solution

898

How It Works

898

The Code

900

17-30. Animate Several Properties in Parallel

901

Problem

901

Solution

902

How It Works

902

The Code

902

17-31. Create a Keyframe-Based Animation

904

Problem

904

Solution

904

How It Works

904

The Code

905

17-32. Animate an Object Along a Path

906

Problem

906

Solution

906

How It Works

907

The Code

908

17-33. Play a Media File

910

Problem

910

Solution

910

How It Works

910

The Code

910

17-34. Query Keyboard State

914

Problem

914

Solution

914

How It Works

915

The Code

915

Index

917