- •Table of Contents
- •C# and the .NET Platform, Second Edition
- •Introduction
- •Part One: Introducing C# and the .NET Platform
- •Part Two: The C# Programming Language
- •Part Three: Programming with .NET Assemblies
- •Part Four: Leveraging the .NET Libraries
- •Part Five: Web Applications and XML Web Services
- •Obtaining This Book's Source Code
- •The .NET Solution
- •What C# Brings to the Table
- •The Role of the Assembly Manifest
- •Summary
- •Chapter 2: Building C# Applications
- •Summary
- •Chapter 3: C# Language Fundamentals
- •Defining Program Constants
- •Defining Custom Class Methods
- •C# Enumerations
- •Summary
- •The Second Pillar: C#'s Inheritance Support
- •Summary
- •Catching Exceptions
- •Finalizing a Type
- •Garbage Collection Optimizations
- •Summary
- •Chapter 6: Interfaces and Collections
- •Building Comparable Objects (IComparable)
- •Summary
- •Summary
- •Internal Representation of Type Indexers
- •Summary
- •An Overview of .NET Assemblies
- •Understanding Delayed Signing
- •Using a Shared Assembly
- •GAC Internals
- •Summary
- •Spawning Secondary Threads
- •A More Elaborate Threading Example
- •Summary
- •Summary
- •Object Persistence in the .NET Framework
- •The .NET Remoting Namespaces
- •Understanding the .NET Remoting Framework
- •All Together Now!
- •Terms of the .NET Remoting Trade
- •Testing the Remoting Application
- •Revisiting the Activation Mode of WKO Types
- •Deploying the Server to a Remote Machine
- •Summary
- •Control Events
- •The Form Class
- •Summary
- •Regarding the Disposal of System.Drawing Types
- •Understanding the Graphics Class
- •Summary
- •The TextBox Control
- •Working with Panel Controls
- •Configuring a Control's Anchoring Behavior
- •Summary
- •Chapter 16: The System.IO Namespace
- •The Static Members of the Directory Class
- •The Abstract Stream Class
- •Summary
- •The Role of ADO.NET Data Providers
- •The Types of System.Data
- •Selecting a Data Provider
- •The Types of the System.Data.OleDb Namespace
- •Working with the OleDbDataReader
- •Summary
- •Submitting the Form Data (GET and POST)
- •Some Benefits of ASP.NET
- •Creating an ASP.NET Web Application by Hand
- •The Composition of an ASP.NET Page
- •The Derivation of an ASP.NET Page
The Static Members of the Directory Class |
|
C# and the .NET Platform, S |
ond Edition |
by Andrew Troelsen |
ISBN:1590590554 |
Now that you have seen the DirectoryInfo type in action, you can learn about the Directory type. By and
Apr ss © 2003 (1200 pages)
large, the members of the Directory mimic the same functionality provided by the instance-level members
This comprehensive text starts with a brief overview of the
defined by DirectoryInfo,C# languagewith andfewthennotablequicklyexceptionsmoves to(GetLogicalDrives()key technical and for one). They also generally return strings ratherarchitecturalthan objectsissues. Dueforto.NETthedeveloperscommon public. interface of each type, I assume you will consult online help to view each member of the Directory class.
TableThisoffinalContentsiteration of the MyDirectory class lists the names of all drives mapped to the current computer C#andandusesthethe.NETstaticPlatform,DirectorySecond.Delete()Editionmethod to remove the \MyFoo and \MyBar\MyQaaz subdirectories
previously created:
Introduction
Part One - Introducing C# and the .NET Platform
Chapterclass1MyDirectory- The Phil sophy of .NET
Chapter{ 2 - Building C# Applications
Part Twopublic- The C#staticProgrammingvoidLanguageMain(String[] args)
Chapter {3 - C# Language Fundamentals
DirectoryInfo dir = new DirectoryInfo(@"C:\WinNT");
Chapter 4 - Object-Oriented Programming with C#
Chapter 5 - Exceptions and Object Lifetime
// Now call some static members of the Directory class.
Chapter 6 - Interfaces and Collections
// List all drives on current computer.
Chapter 7 - Callback Interfaces, Delegates, and Events
string[] drives = Directory.GetLogicalDrives();
Chapter 8 - Advanced C# Type Construction Techniques
Console.WriteLine("Here are your drives:");
Part Three - Programming with .NET Assemblies
foreach(string s in drives)
Chapter 9 - Understanding .NET Assemblies
{
Chapter 10 - Processes, AppDomains, Contexts, and Threads
Console.WriteLine("->{0} ", s);
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
}
Part Four - Leveraging the .NET Libraries
// Delete what was created.
Chapter 12 - Object Serialization and the .NET Remoting Layer
Console.Write("Going to delete\n->" + dir.FullName +
Chapter 13 - Building a Better Window (Introducing Windows Forms)
"\\MyBar\\MyQaaz.\nand\n->" + dir.FullName +
Chapter 14 - A Better Painting Framework (GDI+)
"\\MyFoo.\n" +"Press a key to continue!");
Chapter 15 - Programming with Windows Forms Controls
Console.Read();
Chapter 16 - The System.IO Namespace
try
Chapter 17 - Data{ Access with ADO.NET
Part Five - Web ApplicatDirectoryons and XML.Delete(@"C:\WinNT\MyFoo");Web S rvices
Chapter 18 |
- ASP.NET //WebThePagessecondand WebparameterControls |
specifies if you |
|
Chapter 19 |
- ASP.NET Web// wishApplicatoi nsblow away any internal subdirectories. |
||
Chapter 20 |
- XML WebDirectorySe vices .Delete(@"C:\WinNT\MyBar", true); |
||
Index |
|
} |
|
List of Figures |
catch(IOException e) |
|
|
List of Tables |
{ |
|
Console.WriteLine(e.Message);
}
}
}
Great! At this point you have investigated some core behaviors of the Directory and DirectoryInfo types. Next, you need to learn how to create, open, close, and destroy the files that populate a given directory.
SOURCE The MyDirectoryApp project is located under the Chapter 16 subdirectory.
CODE
The FileInfoC#Classand the .NET Platform, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
The role of the FileInfo class is to obtain a number of details regarding existing files on your hard drive
Apress © 2003 (1200 pages)
(time created, size, file attributes, and so forth) as well as aid in the creation, copying, moving, and
This comprehensive text starts with a brief overview of the
destruction of filesC#. Inlanguageadditionandto thethensetquicklyof functionalitymoves to inheritedkey technicalby FileSystemInfo,and Table 16-5 describes some core membersarchitecturalunique toissuthesFileInfofor .NETclassdevelopers. .
Table 16-5: FileInfo Core Members
Table of Contents |
|
SecondMeaningEditionin Life |
|
||
C#FileInfoand the .NET Platform, |
|
|
|||
IntroductionMember |
|
|
|
||
|
|
|
|
|
|
Part One - Introducing C# |
and the .NET Platform |
|
|||
|
AppendText() |
|
Creates a StreamWriter type (described later) that appends text to a file |
|
|
|
Chapter 1 |
- The Philosophy |
of .NET |
|
|
|
|
||||
|
ChapterCopyTo()2 - Building C# |
ApplicationsCopies an existing file to a new file |
|
||
|
|
|
|
|
|
Part Two - The C# Programming Language |
|
||||
|
Create() |
|
|
Creates a new file and returns a FileStream type (described later) to |
|
Chapter 3 |
- C# Language Fundamentals |
|
|||
|
|
|
|
interact with the created file |
|
|
Chapter 4 |
- Object-Oriented Programming with C# |
|
||
|
|
||||
|
ChapterCreateText()5 - Exceptions |
andCreatesObjectaLifetimeStreamWriter type that writes a new text file |
|
||
|
|
|
|
|
|
|
Chapter 6 |
- Interfaces and Collections |
|
||
|
Delete() |
|
|
Deletes the file to which a FileInfo instance is bound |
|
|
Chapter 7 |
- Callback Interfaces, Delegates, and Events |
|
||
|
|
||||
|
Directory |
|
Gets an instance of the parent directory |
|
|
|
Chapter 8 |
- Advanced C# Type Construction Techniques |
|
||
|
|
|
|
|
|
Part Three - Programming |
|
with .NET Assemblies |
|
||
|
DirectoryName |
|
Gets the full path to a file |
|
|
|
Chapter 9 |
- Understanding |
|
.NET Assemblies |
|
|
|
|
|||
|
Length |
- Processes, |
|
Gets the size of the current file or directory |
|
|
Chapter 10 |
|
AppDomains, Contexts, and Threads |
|
|
|
|
|
|
|
|
|
Chapter 11 |
- Type Reflection, Late Binding, and Attribute-Based Programming |
|
||
|
MoveTo() |
|
Moves a specified file to a new location, providing the option to specify a |
|
|
Part Four - Leveraging the .NET Libraries |
|
||||
|
|
|
|
new filename |
|
|
|
|
|
||
|
Chapter 12 |
- Object Serialization and the .NET Remoting Layer |
|
||
|
Name |
|
|
Gets the name of the file |
|
|
Chapter 13 |
- Building a |
|
Better Window (Introducing Windows Forms) |
|
|
|
||||
|
Chapter 14 |
- A Better Painting Framework (GDI+) |
|
||
|
Open() |
|
|
Opens a file with various read/write and sharing privileges |
|
|
|
|
|
|
|
|
Chapter 15 |
- Programming |
|
with Windows Forms Controls |
|
|
OpenRead() |
|
Creates a read-only FileStream |
|
|
|
Chapter 16 |
- The System.IO |
Namespace |
|
|
|
|
||||
|
|
|
|
|
|
ChaptOpenText()r 17 - Data Access withCreatesADO.aNETStreamReader type (described later) that reads from an existing
Part Five - Web ApplicationstextandfileXML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
OpenWrite() |
Creates a read/write FileStream type |
Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
First, you should be aware that many methods defined by FileInfo return a specific stream-centric type
Index
(FileStream, StreamWriter, StreamReader, and so forth) that allows you to begin reading and writing data
List of Figures
to (or reading from) the associated file in a variety of ways. We'll check out these types later in this
List of Tables
chapter; however, until then, the following code block illustrates the most generic (and least flexible) way to create a file programmatically:
public class FileManipulator
{
public static int Main(string[] args)
{
// Make a new file on the C: drive.
FileInfo f = new FileInfo(@"C:\Test.txt");
FileStream fs = f.Create();
// Print some basic traits of the test.txt file.
Console.WriteLine("Creation: {0} ", f.CreationTime); Console.WriteLine("Full name: {0} ", f.FullName); Console.WriteLine("Full atts: {0} ", f.Attributes.ToString()); Console.Write("Press a key to delete file");
Console.Read();
// Close the file stream and delete the file.
C# and the .NET Platform, Second Edition fs.Close();
|
|
by Andrew Troelsen |
ISBN:1590590554 |
|
|
|
f.Delete(); |
|
|
|
|
Apress © 2003 (1200 pages) |
|
|
|
} |
return 0; |
|
|
|
This comprehensive text starts with a brief overview of the |
|||
} |
C# language and then quickly moves to key technical and |
|||
|
||||
|
architectural issues for .NET developers. |
|
Notice that the FileInfo.Create() method returns a FileStream type that allows you to close the new file
Table of Contents
before removing it from the hard drive. (You will see additional uses of FileStream later in the chapter.)
C# and the .NET Platform, Second Edition
When you run this application, you will be able to find your newly created *.txt file under the C drive.
Introduction
Part One - Introducing C# and the .NET Platform
Examining the FileInfo.Open() Method
Chapter 1 - The Philosophy of .NET
Chapter 2 - Building C# Applications
The FileInfo.Open() method can be used to open existing files as well as create new files with far more
Part Two - The C# Programming Language
precision than FileInfo.Create(). To illustrate, ponder the following logic:
Chapter 3 - C# Language Fundamentals
Chapter 4 - Object-Oriented Programming with C#
// Open (or create) a file with read/write attributes (no sharing),
Chapter 5 - Exceptions and Object Lifetime
// and store file handle in a FileStream object.
Chapter 6 - Interfaces and Collections
FileInfo f2 = new FileInfo(@"C:\HelloThere.ini");
Chapter 7 - Callback Interfaces, Delegates, and Events
FileStream s = f2.Open(FileMode.OpenOrCreate,FileAccess.ReadWrite,
Chapter 8 - Advanced C# Type Construction Techniques
FileShare.None);
Part Three - Programming with .NET Assemblies
...
Chapter 9 - Understanding .NET Assemblies
s.Close();
Chapter 10 - Processes, AppDomains, Contexts, and Threads
f2.Delete();
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
Part Four - Leveraging the .NET Libraries
This version of the overloaded Open() method requires three parameters. The first parameter specifies
Chapter 12 - Object Serialization and the .NET Remoting Layer
the general flavor of the open request (e.g., make a new file, open an existing file, append to a file, and so
Chapter 13 - Building a Better Window (Introducing Windows Forms)
on), which is specified using the FileMode enumeration (Table 16-6).
Chapter 14 - A Better Painting Framework (GDI+)
Chapter 15 - Programming with Windows Forms Controls
|
Table 16-6: FileMode Enumeration Values |
|||||
|
Chapter 16 - The System.IO Namespace |
|||||
|
|
|
|
|
|
|
|
|
Chapter 17 - Data Access with ADO.NET |
|
|
||
|
|
FileMode |
|
Meaning in Life |
|
|
Part Five - Web Applications and XML Web Services |
|
|
||||
|
|
Enumeration |
|
|
|
|
Chapter 18 - ASP.NET Web Pages and Web Controls |
|
|
||||
|
|
Value |
|
|
|
|
|
Chapter 19 - ASP.NET Web |
|
Applications |
|
|
|
|
|
|
||||
|
|
Append |
|
Opens the file if it exists and seeks to the end of the file. If the specified |
|
|
|
Chapter 20 - XML Web Services |
|
|
|||
Index |
|
file does not exist, a new file is created. Be aware that FileMode.Append |
|
|
||
|
can only be used in conjunction with FileAccess.Write. |
|
|
|||
List of Figures |
|
|
|
|||
|
|
|
|
|||
|
|
Create |
|
Specifies that the operating system should create a new file. Be very |
|
|
List of Tables |
|
|
|
|
||
|
|
|
|
aware that if the file already exists, it is overwritten! |
|
|
|
|
|
|
|
||
|
|
CreateNew |
|
Specifies that the operating system should create a new file. If the file |
|
|
|
|
|
|
already exists, an IOException is thrown. |
|
|
|
|
|
|
|
||
|
|
Open |
|
Specifies that the operating system should open an existing file. |
|
|
|
|
|
|
|
||
|
|
OpenOrCreate |
|
Specifies that the operating system should open a file if it exists; |
|
|
|
|
|
|
otherwise, a new file should be created. |
|
|
|
|
|
|
|
||
|
|
Truncate |
|
Specifies that the operating system should open an existing file. Once |
|
|
|
|
|
|
opened, the file should be truncated so that its size is zero bytes. |
|
|
|
|
|
|
|
|
|
The second parameter, a value from the FileAccess enumeration, is used to determine the read/write behavior of the underlying stream (Table 16-7).
Table 16-7: FileAccess Enumeration Values
C# and the .NET Platform, Second Edition
|
FileAccess |
by Andrew TroelsenMeaning in Life |
ISBN:1590590554 |
||
|
EnumerationApressValue© 2003 (1200 pages) |
|
|||
|
|
|
|
|
|
|
Read |
This comprehensive |
|
text starts with a brief overview of the |
|
|
C# language and |
|
Specifies read-only access to the file (i.e., data can only be |
||
|
|
|
then quickly moves to key technical and |
||
|
|
architectural issues |
|
obtainedfor .NET developers.from the file) |
|
|
|
|
|
|
|
|
ReadWrite |
|
|
Specifies read and write access to the file (i.e., data can be |
|
Table of Contents |
|
|
added to or obtained from the file) |
||
|
|
|
|
||
C#Writeand the .NET Platform, Second EditionSpecifies write access to the file (i.e., data can only be added to |
|||||
Introduction |
|
|
the file) |
|
|
|
|
|
|
|
|
Part One - Introducing C# and the .NET Platform
ChapterFinally,1you- haveThe Philosophythe third parameterof .NET (FileShare), which specifies how the currently open file is to be
shared among other file handlers (Table 16-8).
Chapter 2 - Building C# Applications
Part Two - The C# Programming Language
Table 16-8: FileShare Enumeration Values
Ch pter 3 - C# Language F ndamentals
|
|
|
|
|
|
|
|
|
|
|
Chapter |
4 |
- Object-Oriented |
|
Programming with C# |
|
|
|
|
FileShare |
|
Meaning in Life |
|
|
||
|
|
Chapter |
5 |
- Exceptions and |
|
Object Lifetime |
|
|
|
|
Enumeration |
|
|
|
|
||
|
|
Chapter |
6 |
- Interfaces and |
|
Collections |
|
|
|
|
Value |
|
|
|
|
|
|
|
|
Chapter |
7 |
- Callback Interfaces, Delegates, and Events |
|
|
||
|
|
|
||||||
|
|
ChapterNone |
8 |
- Advanced C# TypeDeclinesConstructionsharingTechniquesof the current file. Any request to open the file (by this |
|
|
||
Part Three - Programming withprocess.NET Assembliesor another process) fails until the file is closed. |
|
|
||||||
|
|
|
|
|
|
|
||
|
|
Chapter |
9 |
- Understanding |
|
.NET Assemblies |
|
|
|
|
Read |
|
|
|
Allows subsequent opening of the file for reading. If this flag is not |
|
|
|
Chapter |
10 |
- Processes, AppDomains, Contexts, and Threads |
|
|
|||
|
|
|
|
|
|
specified, any request to open the file for reading (by this process or |
|
|
|
Chapter |
11 |
- Type Reflection, Late Binding, and Attrib te-Based Programming |
|
|
|||
|
|
|
|
|
|
another process) fails until the file is closed. |
|
|
|
|
|
|
|
||||
Part Four - Leveraging the .NET Libraries |
|
|
||||||
|
|
ReadWrite |
|
Allows subsequent opening of the file for reading or writing. If this flag is |
|
|
||
Chapter |
12 |
- Object Serialization and the .NET Remoting Layer |
|
|
||||
Chapter |
13 |
|
|
not specified, any request to open the file for writing or reading (by this |
|
|
||
- Building a Better Window (Introducing Windows Forms) |
|
|
||||||
|
|
|
|
|
|
process or another process) fails until the file is closed. |
|
|
|
Chapter |
14 |
- A Better Painting |
|
Framework (GDI+) |
|
|
|
|
|
Write |
15 |
- Programming |
|
Allows subsequent opening of the file for writing. If this flag is not |
|
|
|
Chapter |
with Windows Forms C ntrols |
|
|
||||
|
Chapter |
16 |
- The System.IO |
Namespacespecified, any request to open the file for writing (by this process or |
|
|
||
|
Chapter |
17 |
- Data Access withanotherADO.NETprocess) fails until the file is closed. |
|
|
|||
|
|
|
|
|
|
|
|
|
Part Five - Web Applications and XML Web Services
Chapter 18 - ASP.NET Web Pages and Web Controls
ChaptTherFileInfo19 - ASP.NET.OpenRead()Web Applications and FileInfo.OpenWrite() Members
Chapter 20 - XML Web Services
IndexIn addition to the Open() method, the FileInfo class also has members named OpenRead() and ListOpenWrite()of Figur s . As you would imagine, these methods return a properly configured read-only or write-only
FileStream type:
List of Tables
// Get a FileStream object with read-only permissions.
FileInfo f3 = new FileInfo(@"C:\boot.ini");
FileStream readOnlyStream = f3.OpenRead();
...
readOnlyStream.Close();
// Now get a FileStream object with write-only permissions.
FileInfo f4 = new FileInfo(@"C:\config.sys");
FileStream writeOnlyStream = f4.OpenWrite();
...
writeOnlyStream.Close();
The FileInfo.OpenText(), FileInfo.CreateText(), and
FileInfo.AppendText() Members
C# and the .NET Platform, Second Edition
by Andrew Troelsen |
ISBN:1590590554 |
Another "open-centric" member of the FileInfo type is OpenText(). Unlike Open(), OpenRead(), and
Apress © 2003 (1200 pages)
OpenWrite(), the OpenText() method returns an instance of the StreamReader type, rather than a
This comprehensive text starts with a brief overview of the
FileStream-derived type:
C# language and then quickly moves to key technical and architectural issues for .NET developers.
// Get a StreamReader object.
FileInfo f5 = new FileInfo(@"C:\bootlog.txt");
Table of Contents
StreamReader sreader = f5.OpenText();
C#...and the .NET Platform, Second Edition
Intsreaderoduction.Close();
Part One - Introducing C# and the .NET Platform
Chapter 1 - The Philosophy of .NET
The final two methods of interest at this point are CreateText() and AppendText(), both of which return a
Chapter 2 - Building C# Applications
StreamWriter reference, as shown here:
Part Two - The C# Programming Language
Chapter 3 - C# Language Fundamentals
// Get some StreamWriters.
Chapter 4 - Object-Oriented Programming with C#
FileInfo f6 = new FileInfo(@"D:\AnotherTest.txt");
Chapter 5 - Exceptions and Object Lifetime
f6.Open(FileMode.Create, FileAccess.ReadWrite);
Chapter 6 - Interfaces and Collections
StreamWriter swriter = f6.CreateText();
Chapter 7 - Callback Interfaces, Delegates, and Events
...
Chapter 8 - Advanced C# Type Construction Techniques
swriter.Close();
Part Three - Programming with .NET Assemblies
FileInfo f7 = new FileInfo(@"D:\FinalTest.txt");
Chapter 9 - Understanding .NET Assemblies f7.Open(FileMode.Create, FileAccess.ReadWrite);
Chapter 10 - Processes, AppDomains, Contexts, and Threads
StreamWriter swriterAppend = f7.AppendText();
Chapter 11 - Type Reflection, Late Binding, and Attribute-Based Programming
...
Part Four - Leveraging the .NET Libraries
swriterAppend.Close();
Chapter 12 - Object Serialization and the .NET Remoting Layer Chapter 13 - Building a Better Window (Introducing Windows Forms)
ChapterAt this point,14 - AyouBetterhavePaintinggoodFrameworkfeel for the(GDI+)functionality provided by the FileInfo type. (You will see exactly Chapterwhat to15do-withProgrammingthe FileStream,with WindowsStreamReader,Forms Controlsand StreamWriter types shortly.)
Chapter 16 - The System.IO Namespace
Note Be aware that the File type provides almost identical functionality using a number of static
Chapter 17 - Data Access with ADO.NET
members. You will see the File type in action where appropriate, but be sure to check out online
Part Five - Web Applications and XML Web Services
help for an exhaustive listing of each member.
Chapter 18 - ASP.NET Web Pages and Web Controls Chapter 19 - ASP.NET Web Applications
Chapter 20 - XML Web Services
Index
List of Figures
List of Tables