.NET Examples

This page includes a list of .NET programming examples, with source code. Some of these are pretty old, but they remain here just for the sake of tradition! Some of them are newer. This page was last updated in May 2011.


Office Stuff

Using ADO.NET with Excel

This is a console app that uses ADO.NET to generate a new .xls file. No automation.
Sept 2003

Source code

Using Automation to insert SQL data into Excel

This is a console app that uses automation (via the Interop Assemblies) to add QueryTable that inserts data into Excel from SQL Server.
Sept 2003

Source code

Web services

Zipcodes

A web service that maps US zipcodes to city+state pairs. Implemented in .NET. Source code is available, as is a working example.
April 2003

Zipservice home page

Book Info

A web service that does keyword searches for book titles, and also maps from ISBN codes to titles, and does lookups on pricing at online book sellers.
April 2003

Book service home page

Windows Forms

IE Cache Viewer

This is a WinForms app that uses pInvoke to interrogate and modify the contents of the IE Cache. This sample illustrates some interesting winforms things:
2005 January

Download the VS2003 project
Screen shot

Downloadable Control

This is a UserControl that can be hosted in a Windows Forms smart client standalone, or within IE. The control embeds a RichTextBox, and upon loading, the control tries to interrogate the CodeBase and Location of its assembly, and display those results. This interrogation may be prohibited by .NET security settings.
April 2003

Source code
Working example - hosted in IE
A second example - with a versioned control, hosted in IE

WMI

Set Default Printer

This is a small sample that illustrates how to use WMI to get and set the default printer on Win32. This is a console app but the code can easily be adapted into a winforms app.
Sept 2003

Source code

XML Things

Consuming XML from an HTTP feed

This example shows how to consume XML obtained from an HTTP URL, within an ASP.NET page. The XML can be generated by "anything." In this example, an ASP.NET page consumes XML generated by one of (a) a locally hosted ASP.NET page, (b) a remotely-hosted ASP.NET page, or (c) a remotely-hosted JSP page. There was a prior version of this example that used the NASDAQ xml quote service, but apparently Nasdaq.com took that service offline (as of February 2004). In this example, the XML is generated from a query on a SQL Server database. The consuming ASP.NET page retrieves the XML, does a XSL Transform on it, then displays the resulting HTML. As an illustration, there is also an example that does the converse: in other words it's a JSP page that consumes XML from an ASPX.


2004 March

Run the sample
View the source

MQSeries

MQ Load

Simple console app that creates an MQ Queue, then loads 500 messages onto it.
published February 2006

source code

MQ Inquire Queue Names

This is a tiny command-line app, implemented in C#, that uses PCF in the MQ Classes for .NET, to inquire the queue names on a MQ QM. PCF is not supported in the MQ Classes for .NET for MQ v6.0, but it works anyway.


published February 2006

source code

MQ ADSI Explorer

This is a simple WinForms explorer app that uses ADSI (via COM interop) to explore MQSeries. Starting with version 5.1? of IBM MQSeries, Mq exposes an ADSI provider. By this means, MQ objects, including queues, channels, and connections, are queryable and settable via ADSI. This app illustrates how to do it.

originally published April 2003, updated November 2004

Source code (VS 2003 project)
Screen shot image

MQ Postcard

MQSeries ships with a "postcard" app that can be used to verify the installation of mqseries. This is an implementation of the MQ postcard app in C#, using the "MQ Classes for .NET" class libary, that originally shipped in MQ 5.3 CSD05 on Windows in October 2003. (The ma7p supportpac from IBM, published in February 2003, was the precursor of this class library). This postcard app interoperates with the postcard apps IBM has supplied (one written in Java, and another shipped only in binary form).

December 2005

Source code (VS 2003 project)

MQ PCF

IBM's "MQ Classes for .NET" class library that ships with MQ includes undocumented PCF support. This example shows how to check for the existence of a queue, and create a queue, programmatically, using that PCF stuff.

December 2005

Source code

Regex

Regular Expression Verifier

When I build Regex's , I use a visual tool like Regex Designer - by Chris Sells.

But still, the testing of all of the combinations of input is not easy with this sort of visual tool. A generic regex test harness is useful. This is that tool. The output is in XML to allow scripting.

October 2003

Source code

Windows Forms-based Regex Tester

When I want to build a regex, it's nice to be able to dynamically test my trials and iterations. This is a tiny tool that helps out with that.


April 2011

Source code

Tar in .NET

A Tar library and EXE, in C#

.NET lacks a built-in library to handle TAR archives. This library reads and writes tar archives and compressed (gzip'd) tar archives.

Tar is implemented in C#, in the source file Tar.cs. It requires .NET 3.5 at a minimum.

If you compile the Tar.cs module with the symbol "EXE" defined (/d:EXE on the csc.exe command line), then the result will be a console-based Tar application, much like the one shipped in Unix. Compiled this way, it has no dependencies on an external Tar dll.


If you compile the Tar.cs module with no EXE symbol defined, then the result will be a Tar dll, Ionic.Tar.dll, that provides a Tar class, that can then be used from other applications.

The supplied makefile builds both of these targets by default.

March 2010

Source code

Interesting Javascript things

JS-unzip: an Unzip library implemented in Javascript

This library provides that capability a general-purpose unzip capability, for Javascript.

All general-purpose compression algorithms, including the DEFLATE algorithm that is used by the ZIP format for compression, deal with byte-oriented data. But Javascript and the XMLHttpRequest object have been traditionally optimized for handling text data. As a result of that, it's uncommon to see application-level compression and decompression, done in Javascript within the browser. Even so, it is possible, and even straightforward, if you know what to do.

What follows here is a demonstration that shows how to unpack and extract a zipfile, within the context of the browser. This isn't a general purpose unzip. In fact this unzip demonstration does not (and cannot) create filesystem files. It merely downloads a zipfile, peeks inside it, and displays the decompressed contents of that zip file in the browser window. What takes .25s on a desktop takes 3 seconds in a browser. For that reason, some might say that implementing an unzip in javascript is impractical. Maybe. In any case, here it is.

Works in IE8 and Firefox 3.5/3.6. I didn't test in other browsers.

March 2010
Updated May 2011


Working example
Source code

AES in Javascript

This page demonstrates how to perform AES encryption from within javascript running in a browser page. It does RFC2898 key derivation (PBKDF2), and uses a salt and a variable keysize.

The encrypted texts can be decrypted with any AES encryption suite, including the RijndaelManaged class within .NET.

To get the source code and see how it's done, use "View Source".

Works in IE8 and Firefox 3.5/3.6. I didn't test in other browsers.

May 2011

Working example

DES in Javascript

This page demonstrates how to perform DES and 3DES encryption from within javascript running in a browser page. It does RFC2898 key derivation (PBKDF2), and uses a salt and a variable keysize. It is similar to the AES encryption example (see above).

The encrypted texts can be decrypted with any DES encryption suite, including the Des class included in the .NET base class library.

To get the source code and see how it's done, use "View Source".

Works in IE8 and Firefox 3.5/3.6. I didn't test in other browsers.

May 2011

Working example

AES

Integrating SlowAES with .NET AES

The encryption support in .NET is pretty good. In .NET 2.0 Microsoft added support for AES and Rijndael algorithms. There are fully-managed implementations, as well as native implementations that are available via .NET wrappers.

It's easy to encrypt and decrypt in .NET, but a typical scenario involves encrypting in .NET and then decrypting in some other platform. One example is Javascript, for apps that run in a browser. Another example is a COM tool, that does not have access to .NET - say a VB6 application, or a Microsoft Excel spreadsheet.

SlowAES is a nice javascript library for performing AES encryption (not mine). It works well and is easy to use. But it doesn't include the complementary things you need for real-world encryption. It lacks an RFC 2898 PBKDF2 (key generator), for example.

The question becomes, how to integrate SlowAES and .NET's AES?

This example shows how. It shows:

March 2010

Source code

Miscellaneous Others

PDF Creation in .NET

Creating PDFs can be handy. Doing it from .NET seems like a broad need. Here are some examples for how to do it, using the itextsharp library.

published February 2006

Source code for simple example (console app)
Source code for nested tables (console app)
SudokuToy (Winforms app, VS2003 Solution), produces Sudoku puzzle in PDF.

Morpho

Didya ever wonder?... why DataGrids and other data-bound controls can use arrays of objects as data sources, but they display only public properties (not fields)? On the other hand, types generated by wsdl.exe generally export fields (not properties).

Suppose you want to invoke a web services method and display the result in a databound control. Or suppose you want to use XML Serialization, and display the result into a data-bound control. What are you gonna do?

But wait! There is a better way.

This sample shows a general solution for how to morph types with public fields - something you might get from a web service or from XML Serialization - into types with public properties - something a data-bound control can use. The approach is to dynamically generate a type, at runtime, and then convert the array of type1 into an array of type2, and use type2 as the DataSource for the data-bound control. eg,

// get array of types with public fields glue.webservice.OrderInfo[] oia= wsproxy.getOrdersForCustomer(TextBox1.Text); // generate a morphed array suitable for use in a DataGrid: DataGrid1.DataSource= MorphArray(oia);

Because this example uses System.CodeDom to generate the type, it also serves as a short illustration of how to use that nifty technology.

December 2002

Source code

Wake-On-Lan

This console app sends a Wake-on-LAN Magic packet to the given MAC address.

October 2003

Source code

Assembly Interrogator

This simple Windows Forms app loads an assembly from a local DLL or from a URL, then interrogates the assembly for its version, culture, codebase, location, etc; and then displays the results. It also itemizes the types available in the assembly.

April 2003

Source code

Data Copier

Did you ever come upon the situation where you want to fill a DataSet from one database, but then update another database with the data? This simple example shows how to associate different connections to the Update command and the Select command for a single DataAdapter, thereby enabling this scenario. The example works with SQL Server, but could be modified to work with any 2 databases that share a common DataAdapter (eg, they are both accessed from the OledbDataAdapter).

You could also modify this sample to use 2 distinct DataAdapters.

February 2003

Source code

This page was last updated Tuesday, 10 May 2011
This page has been accessed 39811 times since 18 September 2003