SlideShare a Scribd company logo
1 of 21
FILE IO
System.IO Namespace
• Provides basic file and directory support classes
• Contains types that enable you to read and write files and data streams
• Many of the types or classes defined as part of the System.IO namespace
are designed around streams
2
System.IO Namespace (continued)
C# Programming: From Problem Analysis to Program Design
3
System.IO Namespace (continued)
4
Many are
exception
classes that
can be
thrown
while
accessing
information
using
streams,
files and
directories
5
.NET file class hierarchy
File and Directory Classes
• Utility classes allow you to manipulate files and directory
structures
• copying, moving, renaming, creating, opening,
deleting, and appending files
• Expose only static members
• Objects are not instantiated from these classes
• To invoke the method, the method name is
preceded by the class name (as opposed to an
object’s name)
File.Copy(“sourceFile”, “targetFile”); 6
File Class
7
File Class (continued)
• One static method of the File class is Exists( )
Example 12-1
/* DirectoryStructure.cs illustrates using File and Directory utilities. */
using System;
using System.IO;
class DirectoryStructure
{
public static void Main( )
{
string fileName = "BirdOfParadise.jpg";
if (File.Exists(fileName))
{
8
File Class (continued)
• GetAttritubes( ) returns a FileAttributes enumeration
• Enumeration is a special form of value type that supplies alternate names
for the values of an underlying primitive type
• Enumeration type has a name, an underlying type, and a set of
fields
9
File Class (continued)
Console.WriteLine( "FileName: {0}", fileName );
Console.WriteLine( "Attributes: {0}", File.GetAttributes(fileName) );
Console.WriteLine( "Created: {0}", File.GetCreationTime( fileName ) );
Console.WriteLine( "LastAccessed: {0}",File.GetLastAccessTime
( fileName ) );
10
Figure 12-3 Output from the DirectoryStructure application
GetAttributes( )
returns
enumeration
Directory Class
• Static methods for creating and moving through directories and
subdirectories
11
Directory Class (continued)
12
DirectoryInfo and FileInfo Classes
• Add additional functionality beyond File and Directory classes
• Difference – Both have instance methods instead of static
members
• Both have public properties and public constructors
• Neither can be inherited
13
14
DirectoryInfo
• Adds two other key properties, Parent and Root
• Parent gets the parent directory of a specified subdirectory
• Root gets the root portion of a path
• Be careful with paths; they must be well-formed or an exception is
raised
DirectoryInfo dir = new DirectoryInfo(".");
Console.WriteLine("Current Directory: n{0}n",
Directory.GetCurrentDirectory( ));
15
File Streams
• Several abstract classes for dealing with files
• Stream,TextWriter, andTextReader
• Stream classes provide generic methods for dealing with
input/output
• IO.Stream class and its subclasses – byte-level
data
• IO.TextWriter and IO.TextReader – data in a text
(readable) format
• StreamReader and StreamWriter derived classes
of IO.TextWriter and IO.TextReader 16
File Streams (continued)
• StreamWriter class for write data to text file
• Includes implementations forWrite( ) andWriteLine( )
• StreamReader class to read or and from text files
• Includes implementations of Read( ) and ReadLine( )
• System.IO namespace
• Using System.IO;
17
File Streams (continued)
StreamWriter outputFile = new
StreamWriter("someOutputFileName");
StreamReader inputFile = new
StreamReader("someInputFileName");
• outputFile and inputFile represent the file stream objects
• Actual file names are “someOutputFileName” and
“someInputFileName” – inside double quotes
• Place file extensions such as .dat, .dta, or .txt onto the end of
actual filename when it is created
18
File Streams (continued)
• UseWrite( ) orWriteLine( ) with the instantiated stream
object
outputFile.WriteLine("This is the first line in a text file");
• Use Read( ) or ReadLine( ) with the instantiated stream
object
string inValue = inputFile.ReadLine( );
19
File Streams (continued)
20
C# File IO Operations

More Related Content

What's hot

This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonmoazamali28
 
SQL: Creating and Altering Tables
SQL: Creating and Altering TablesSQL: Creating and Altering Tables
SQL: Creating and Altering TablesRJ Podeschi
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection frameworkankitgarg_er
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Aijaz Ali Abro
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#Adeel Rasheed
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued Hitesh-Java
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java pptkunal kishore
 

What's hot (20)

This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
SQL: Creating and Altering Tables
SQL: Creating and Altering TablesSQL: Creating and Altering Tables
SQL: Creating and Altering Tables
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Oop java
Oop javaOop java
Oop java
 

Similar to C# File IO Operations

Similar to C# File IO Operations (20)

File system
File systemFile system
File system
 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
 
File system in operating system e learning
File system in operating system e learningFile system in operating system e learning
File system in operating system e learning
 
Intake 38 10
Intake 38 10Intake 38 10
Intake 38 10
 
File System.pptx
File System.pptxFile System.pptx
File System.pptx
 
Intake 37 11
Intake 37 11Intake 37 11
Intake 37 11
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
File system1.pptx
File system1.pptxFile system1.pptx
File system1.pptx
 
Os6
Os6Os6
Os6
 
SQL Server 2012 - FileTables
SQL Server 2012 - FileTables SQL Server 2012 - FileTables
SQL Server 2012 - FileTables
 
C# Framework class library
C# Framework class libraryC# Framework class library
C# Framework class library
 
File System operating system operating system
File System  operating system operating systemFile System  operating system operating system
File System operating system operating system
 
CNIT 121: 13 Investigating Mac OS X Systems
CNIT 121: 13 Investigating Mac OS X SystemsCNIT 121: 13 Investigating Mac OS X Systems
CNIT 121: 13 Investigating Mac OS X Systems
 
10 File System
10 File System10 File System
10 File System
 
Workshops
WorkshopsWorkshops
Workshops
 
Systems Programming - File IO
Systems Programming - File IOSystems Programming - File IO
Systems Programming - File IO
 
25dom
25dom25dom
25dom
 
File system interface
File system interfaceFile system interface
File system interface
 
File management in OS
File management in OSFile management in OS
File management in OS
 
CNIT 152: 13 Investigating Mac OS X Systems
CNIT 152: 13 Investigating Mac OS X SystemsCNIT 152: 13 Investigating Mac OS X Systems
CNIT 152: 13 Investigating Mac OS X Systems
 

More from Prem Kumar Badri

Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexersPrem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopePrem Kumar Badri
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and eventsPrem Kumar Badri
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objectsPrem Kumar Badri
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variablesPrem Kumar Badri
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsPrem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingPrem Kumar Badri
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsPrem Kumar Badri
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parametersPrem Kumar Badri
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variablesPrem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformPrem Kumar Badri
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collectionPrem Kumar Badri
 

More from Prem Kumar Badri (20)

Module 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributes
 
Module 14 properties and indexers
Module 14 properties and indexersModule 14 properties and indexers
Module 14 properties and indexers
 
Module 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scopeModule 12 aggregation, namespaces, and advanced scope
Module 12 aggregation, namespaces, and advanced scope
 
Module 13 operators, delegates, and events
Module 13 operators, delegates, and eventsModule 13 operators, delegates, and events
Module 13 operators, delegates, and events
 
Module 11 : Inheritance
Module 11 : InheritanceModule 11 : Inheritance
Module 11 : Inheritance
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
Module 9 : using reference type variables
Module 9 : using reference type variablesModule 9 : using reference type variables
Module 9 : using reference type variables
 
Module 8 : Implementing collections and generics
Module 8 : Implementing collections and genericsModule 8 : Implementing collections and generics
Module 8 : Implementing collections and generics
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
 
Module 5 : Statements & Exceptions
Module 5 : Statements & ExceptionsModule 5 : Statements & Exceptions
Module 5 : Statements & Exceptions
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
 
Module 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#
 
Module 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET PlatformModule 1 : Overview of the Microsoft .NET Platform
Module 1 : Overview of the Microsoft .NET Platform
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
 
C# Multi threading
C# Multi threadingC# Multi threading
C# Multi threading
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collections
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

C# File IO Operations

  • 2. System.IO Namespace • Provides basic file and directory support classes • Contains types that enable you to read and write files and data streams • Many of the types or classes defined as part of the System.IO namespace are designed around streams 2
  • 3. System.IO Namespace (continued) C# Programming: From Problem Analysis to Program Design 3
  • 4. System.IO Namespace (continued) 4 Many are exception classes that can be thrown while accessing information using streams, files and directories
  • 5. 5 .NET file class hierarchy
  • 6. File and Directory Classes • Utility classes allow you to manipulate files and directory structures • copying, moving, renaming, creating, opening, deleting, and appending files • Expose only static members • Objects are not instantiated from these classes • To invoke the method, the method name is preceded by the class name (as opposed to an object’s name) File.Copy(“sourceFile”, “targetFile”); 6
  • 8. File Class (continued) • One static method of the File class is Exists( ) Example 12-1 /* DirectoryStructure.cs illustrates using File and Directory utilities. */ using System; using System.IO; class DirectoryStructure { public static void Main( ) { string fileName = "BirdOfParadise.jpg"; if (File.Exists(fileName)) { 8
  • 9. File Class (continued) • GetAttritubes( ) returns a FileAttributes enumeration • Enumeration is a special form of value type that supplies alternate names for the values of an underlying primitive type • Enumeration type has a name, an underlying type, and a set of fields 9
  • 10. File Class (continued) Console.WriteLine( "FileName: {0}", fileName ); Console.WriteLine( "Attributes: {0}", File.GetAttributes(fileName) ); Console.WriteLine( "Created: {0}", File.GetCreationTime( fileName ) ); Console.WriteLine( "LastAccessed: {0}",File.GetLastAccessTime ( fileName ) ); 10 Figure 12-3 Output from the DirectoryStructure application GetAttributes( ) returns enumeration
  • 11. Directory Class • Static methods for creating and moving through directories and subdirectories 11
  • 13. DirectoryInfo and FileInfo Classes • Add additional functionality beyond File and Directory classes • Difference – Both have instance methods instead of static members • Both have public properties and public constructors • Neither can be inherited 13
  • 14. 14
  • 15. DirectoryInfo • Adds two other key properties, Parent and Root • Parent gets the parent directory of a specified subdirectory • Root gets the root portion of a path • Be careful with paths; they must be well-formed or an exception is raised DirectoryInfo dir = new DirectoryInfo("."); Console.WriteLine("Current Directory: n{0}n", Directory.GetCurrentDirectory( )); 15
  • 16. File Streams • Several abstract classes for dealing with files • Stream,TextWriter, andTextReader • Stream classes provide generic methods for dealing with input/output • IO.Stream class and its subclasses – byte-level data • IO.TextWriter and IO.TextReader – data in a text (readable) format • StreamReader and StreamWriter derived classes of IO.TextWriter and IO.TextReader 16
  • 17. File Streams (continued) • StreamWriter class for write data to text file • Includes implementations forWrite( ) andWriteLine( ) • StreamReader class to read or and from text files • Includes implementations of Read( ) and ReadLine( ) • System.IO namespace • Using System.IO; 17
  • 18. File Streams (continued) StreamWriter outputFile = new StreamWriter("someOutputFileName"); StreamReader inputFile = new StreamReader("someInputFileName"); • outputFile and inputFile represent the file stream objects • Actual file names are “someOutputFileName” and “someInputFileName” – inside double quotes • Place file extensions such as .dat, .dta, or .txt onto the end of actual filename when it is created 18
  • 19. File Streams (continued) • UseWrite( ) orWriteLine( ) with the instantiated stream object outputFile.WriteLine("This is the first line in a text file"); • Use Read( ) or ReadLine( ) with the instantiated stream object string inValue = inputFile.ReadLine( ); 19