vb.net barcode generator open source C H A P T E R 20 in C#.NET

Printer DataMatrix in C#.NET C H A P T E R 20

C H A P T E R 20
Creating DataMatrix In C#
Using Barcode generator for .NET Control to generate, create DataMatrix image in VS .NET applications.
www.OnBarcode.com
Recognizing ECC200 In Visual C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Files, Streams, and IO
Print Code-128 In C#.NET
Using Barcode creator for VS .NET Control to generate, create Code-128 image in VS .NET applications.
www.OnBarcode.com
Linear 1D Barcode Drawer In Visual C#.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Linear 1D Barcode image in .NET applications.
www.OnBarcode.com
In this chapter, we will look at how C# supports files and IO. These two areas are related because the mechanism that C# uses for reading and writing files is usually accessed through the classes that are used to find files and directories in the first place. All the classes in this directory can be found in the System.IO and System.IO.Compression namespaces. Table 20-1 provides the summary for this chapter. Table 20-1. Quick Problem/Solution Reference for 20
Generate EAN / UCC - 13 In Visual C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create EAN13 image in .NET framework applications.
www.OnBarcode.com
Printing PDF 417 In Visual C#
Using Barcode encoder for .NET framework Control to generate, create PDF417 image in VS .NET applications.
www.OnBarcode.com
Problem
Make UCC.EAN - 128 In C#
Using Barcode generator for Visual Studio .NET Control to generate, create GS1 128 image in .NET applications.
www.OnBarcode.com
Encoding ANSI/AIM Codabar In C#.NET
Using Barcode creation for .NET Control to generate, create Code 2 of 7 image in VS .NET applications.
www.OnBarcode.com
Find the files and directories within a given directory. Get detailed information about a file or directory. Get or set the current working directory of the current process. Read and write data without using streams. Create and manipulate path strings. Monitor a directory for changes. Read and write sequential data in a consistent way from a range of data sources. Stream data in memory. Stream data in files.
Make ECC200 In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
www.OnBarcode.com
Data Matrix ECC200 Creator In Java
Using Barcode printer for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
Solution
Data Matrix ECC200 Creation In Java
Using Barcode generation for Android Control to generate, create Data Matrix image in Android applications.
www.OnBarcode.com
QR Code JIS X 0510 Maker In Java
Using Barcode drawer for Android Control to generate, create Quick Response Code image in Android applications.
www.OnBarcode.com
Use the Directory or DirectoryInfo classes.
PDF 417 Maker In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
USS Code 39 Decoder In Visual Basic .NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Listings
Encoding Barcode In Objective-C
Using Barcode creator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
QR Code Printer In None
Using Barcode generator for Microsoft Excel Control to generate, create QR Code image in Microsoft Excel applications.
www.OnBarcode.com
20-1 through 20-3, 20-9
Code 39 Scanner In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
GS1 - 12 Encoder In Java
Using Barcode creation for Android Control to generate, create UPC-A Supplement 5 image in Android applications.
www.OnBarcode.com
Use the Directory, FileInfo, or DirectoryInfo classes.
Make Matrix Barcode In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
Encode PDF417 In Java
Using Barcode encoder for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
20-4, 20-6, 20-7, 20-8, 20-10 20-5
Use the GetCurrentDirectory and SetCurrentDirectory methods of the Directory class.
Use the convenience methods in the File class.
20-11
Use the Path class.
20-12
Use the FileSystemWatcher class.
20-13 through 20-15 20-16
Use Stream objects.
Use the MemoryStream class. Use the FileStream class.
20-17 20-18
CHAPTER 20 FILES, STREAMS, AND IO
Problem
Manipulate data as it is read from or written to streams. Read and write binary representations of C# built-in types. Read and write string representations of C# types.
Solution
Use a pass-through stream.
Listings
20-19, 20-20
Use the BinaryReader and BinaryWriter classes.
20-21
Use the StreamReader and StreamWriter classes.
20-22, 20-23
Working with Files and Directories
We will start this chapter by looking at the facilities C# provides for working with files and directories. As you will see, there is a lot of duplication in the available classes, and the ones that you choose for use in your program are a matter of personal preference.
Using the System.IO.Directory Class
The System.IO.Directory class has a range of static methods that you can use to interrogate and manipulate files and directories. In the following sections, I ll demonstrate some of the most commonly used methods.
Enumerating Files and Directories
The Directory class contains a set of methods that let you enumerate the contents of a directory. These methods are described in Table 20-2. Table 20-2. Enumeration Methods of System.IO.Directory
Method
GetDirectories(string) GetDirectories(string, string) GetDirectories(string, string, SearchOption)
Description
Returns a string[] containing the names of the directories contained within the directory specified by the first string parameter. If the second string parameter is used, then the contents are filtered to those that contain the parameter value. The third parameter control whether the results should include the contents of subdirectories. Like the GetDirectories method but returns a string array containing file names.
GetFiles(string) GetFiles(string, string) GetFiles(string, string, SearchOption)
CHAPTER 20 FILES, STREAMS, AND IO
Method
GetFileSystemEntries(string) GetFileSystemEntries(string, string) GetFileSystemEntries(string, string, SearchOption)
Description
Like the GetDirectories method but returns a string array containing file and directory names.
Listing 20-1 demonstrates using the basic version of each of the methods described in Table 20-2 to get details of the files and directories of my C:\ directory. Listing 20-1. Getting Information Using the Directory Class using System; using System.IO; class Listing 01 { static void Main(string[] args) { string[] filesArray = Directory.GetFiles(@"C:\"); Console.WriteLine("--- GetFiles Results ---"); foreach (string name in filesArray) { Console.WriteLine("File name: {0}", name); } string[] dirsArray = Directory.GetDirectories(@"C:\"); Console.WriteLine("\n--- GetDirectories Results ---"); foreach (string name in dirsArray) { Console.WriteLine("Directory name: {0}", name); } string[] allArray = Directory.GetFileSystemEntries(@"C:\"); Console.WriteLine("\n--- GetFileSystemEntries Results ---"); foreach (string name in allArray) { Console.WriteLine("FileSystemEntry name: {0}", name); } // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); } } Notice that I have prefixed each path name parameter with the @ symbol. This is to denote the path string as a verbatim literal so that the backslash character (\) in the path parameter is not interpreted as the start of a string escape sequence. You will learn more about verbatim string literals and escape sequences in 16. Compiling and running Listing 20-1 produces the following results on my computer:
CHAPTER 20 FILES, STREAMS, AND IO
--- GetFiles Results --File name: C:\hiberfil.sys File name: C:\pagefile.sys --- GetDirectories Results --Directory name: C:\$Recycle.Bin Directory name: C:\Config.Msi Directory name: C:\Documents and Settings Directory name: C:\MSOCache Directory name: C:\Northwind Database Directory name: C:\PerfLogs Directory name: C:\Program Files Directory name: C:\Program Files (x86) Directory name: C:\ProgramData Directory name: C:\Recovery Directory name: C:\System Volume Information Directory name: C:\Users Directory name: C:\Windows --- GetFileSystemEntries Results --FileSystemEntry name: C:\$Recycle.Bin FileSystemEntry name: C:\Config.Msi FileSystemEntry name: C:\Documents and Settings FileSystemEntry name: C:\hiberfil.sys FileSystemEntry name: C:\MSOCache FileSystemEntry name: C:\Northwind Database FileSystemEntry name: C:\pagefile.sys FileSystemEntry name: C:\PerfLogs FileSystemEntry name: C:\Program Files FileSystemEntry name: C:\Program Files (x86) FileSystemEntry name: C:\ProgramData FileSystemEntry name: C:\Recovery FileSystemEntry name: C:\System Volume Information FileSystemEntry name: C:\Users FileSystemEntry name: C:\Windows Press enter to finish You can see from the results that the path I specified for the search is prepended to the name of each file and directory name. If I had specified a relative path, then this would also have been prepended to the results. Here is an example of using a relative path: string[] filesArray = Directory.GetFiles(@"."); Console.WriteLine("--- GetFiles Results ---"); foreach (string name in filesArray) { Console.WriteLine("File name: {0}", name); } --- GetFiles Results --File name: .\Listing 01.exe
Copyright © OnBarcode.com . All rights reserved.