7: Stepping through Collections in C#

Creator Code 3/9 in C# 7: Stepping through Collections

7: Stepping through Collections
Print ANSI/AIM Code 39 In Visual C#
Using Barcode generator for Visual Studio .NET Control to generate, create USS Code 39 image in .NET applications.
In This
Generate Bar Code In C#
Using Barcode maker for VS .NET Control to generate, create barcode image in Visual Studio .NET applications.
Handling a directory as a collection of files and a file as a collection
Generate Code 3/9 In .NET Framework
Using Barcode creation for VS .NET Control to generate, create Code 39 Full ASCII image in .NET framework applications.
of bytes
Draw Code 3 Of 9 In Visual Basic .NET
Using Barcode generation for .NET Control to generate, create ANSI/AIM Code 39 image in VS .NET applications.
Implementing a LinkedList collection Enumerating, or iterating, LinkedList Implementing an indexer for easy access to collection objects Easily looping through a collection with the C# iterator blocks
EAN / UCC - 13 Encoder In Visual C#
Using Barcode printer for .NET Control to generate, create EAN 13 image in .NET applications.
hapter 6 in this minibook explores the collection classes provided by the NET Framework class library for use with C# and other NET languages As you probably remember, collection classes are constructs in NET that can be instantiated to hold groups of items If you don t remember, you can read 6 for a reminder The first part of this chapter extends the notion of collections a bit For instance, consider the following collections: a file as a collection of lines or records of data, and a directory as a collection of files Thus this chapter builds on both the collection material in 6 of this minibook and the file material in Book III However, the focus in this chapter is on several ways to step through, or iterate, all sorts of collections, from file directories to arrays and lists of all sorts You also see how to write your own collection class, or linked list
Paint Bar Code In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create bar code image in .NET framework applications.
Iterating through a Directory of Files
Make Code 128 Code Set C In Visual C#
Using Barcode maker for VS .NET Control to generate, create USS Code 128 image in .NET applications.
Reading and writing are the basic skills you need to get ahead in this world That s what makes the FileRead and FileWrite programs in Book IV important In some cases, however, you simply want to skim a directory of files, looking for something The following LoopThroughFiles program looks at all files in a given directory, reading each file and dumping out its contents in hexadecimal format to the console (That may sound like a silly thing to do, but this program also demonstrates how to write out a file in a format other than just strings I describe hexadecimal format in the following sidebar, Getting hexed )
Creating Code 3 Of 9 In Visual C#
Using Barcode creator for .NET framework Control to generate, create Code 39 Extended image in .NET applications.
Iterating through a Directory of Files
Barcode Creation In Visual C#
Using Barcode drawer for .NET framework Control to generate, create barcode image in .NET framework applications.
If you run this program in a directory with lots of files, the hex dump can take a while Also, long files take a while to loop through Either pick a directory with few files or stop a lengthy program run by pressing Ctrl+C This command interrupts a program running in any console window
Making GS1 128 In C#
Using Barcode creator for Visual Studio .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
// LoopThroughFiles -- Loop through all files contained in a directory; // this time perform a hex dump, though it could have been anything using System; using SystemIO; namespace LoopThroughFiles { public class Program { public static void Main(string[] args) { // If no directory name provided string directoryName; if (argsLength == 0) { // get the name of the current directory directoryName = DirectoryGetCurrentDirectory(); } else { // otherwise, assume that the first argument // is the name of the directory to use directoryName = args[0]; } ConsoleWriteLine(directoryName); // Get a list of all files in that directory FileInfo[] files = GetFileList(directoryName); // Now iterate through the files in that list, // performing a hex dump of each file foreach(FileInfo file in files) { // Write out the name of the file ConsoleWriteLine( \n\nhex dump of file {0}: , fileFullName); // Now dump the file to the console DumpHex(file); // Wait before outputting next file ConsoleWriteLine( \nenter return to continue to next file ); ConsoleReadLine(); } // That s it! ConsoleWriteLine( \no files left ); // Wait for user to acknowledge the results ConsoleWriteLine( Press Enter to terminate ); ConsoleRead(); }
Bookland EAN Drawer In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create ISBN image in VS .NET applications.
Iterating through a Directory of Files
Printing ECC200 In VB.NET
Using Barcode printer for .NET framework Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
Book I 7
Paint Code-39 In Visual Basic .NET
Using Barcode generation for .NET framework Control to generate, create Code 39 Full ASCII image in .NET applications.
// GetFileList -- Get a list of all files in a specified directory public static FileInfo[] GetFileList(string directoryName) { // Start with an empty list FileInfo[] files = new FileInfo[0]; try { // Get directory information DirectoryInfo di = new DirectoryInfo(directoryName); // That information object has a list of the contents files = diGetFiles(); } catch(Exception e) { ConsoleWriteLine( Directory \ {0}\ invalid , directoryName); ConsoleWriteLine(eMessage); } return files; } // DumpHex -- Given a file, dump out the contents of the file to the console public static void DumpHex(FileInfo file) { // Open the file FileStream fs; BinaryReader reader; try { fs = fileOpenRead(); // Wrap the file stream in a BinaryReader reader = new BinaryReader(fs); } catch(Exception e) { ConsoleWriteLine( \ncan t read from \ {0}\ , fileFullName); ConsoleWriteLine(eMessage); return; } // Iterate through the contents of the file one line at a time for(int line = 1; true; line++) { // Read another 10 bytes across (all that will fit on a single // line) -- return when no data remains byte[] buffer = new byte[10]; // Use the BinaryReader to read bytes // Note: Using the bare FileStream would have been just as easy in this case int numBytes = readerRead(buffer, 0, bufferLength); if (numBytes == 0) { return; } // Write out the data just read, in a single line preceded by line number ConsoleWrite( {0:D3} - , line); DumpBuffer(buffer, numBytes);
Data Matrix ECC200 Creation In Java
Using Barcode generation for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
Bar Code Printer In .NET
Using Barcode creation for ASP.NET Control to generate, create bar code image in ASP.NET applications.
Encoding Code 39 Full ASCII In Visual Studio .NET
Using Barcode generation for .NET framework Control to generate, create USS Code 39 image in .NET applications.
Bar Code Maker In Java
Using Barcode encoder for Java Control to generate, create bar code image in Java applications.
ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. Terms of Use | Privacy Policy