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

Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in JavaOblivionWalker
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and MethodsNilesh Dalvi
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHPVibrant Technologies & Computers
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In JavaArnab Bhaumik
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#Abid Kohistani
 
Strings in Java
Strings in Java Strings in Java
Strings in Java Hitesh-Java
 
OOP java
OOP javaOOP java
OOP javaxball977
 
Oops in Java
Oops in JavaOops in Java
Oops in Javamalathip12
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionPritom Chaki
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#Hemant Chetwani
 

What's hot (20)

Regular Expressions in Java
Regular Expressions in JavaRegular Expressions in Java
Regular Expressions in Java
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
C# basics
 C# basics C# basics
C# basics
 
Types of methods in python
Types of methods in pythonTypes of methods in python
Types of methods in python
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
OOP java
OOP javaOOP java
OOP java
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Java Streams
Java StreamsJava Streams
Java Streams
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Properties and indexers in C#
Properties and indexers in C#Properties and indexers in C#
Properties and indexers in C#
 

Similar to C# File IO Operations

ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7Sisir Ghosh
 
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 learningLavanya Sharma
 
Intake 38 10
Intake 38 10Intake 38 10
Intake 38 10Mahmoud Ouf
 
Intake 37 11
Intake 37 11Intake 37 11
Intake 37 11Mahmoud Ouf
 
File system1.pptx
File system1.pptxFile system1.pptx
File system1.pptxSamar954063
 
SQL Server 2012 - FileTables
SQL Server 2012 - FileTables SQL Server 2012 - FileTables
SQL Server 2012 - FileTables Sperasoft
 
C# Framework class library
C# Framework class libraryC# Framework class library
C# Framework class libraryPrem Kumar Badri
 
File System operating system operating system
File System  operating system operating systemFile System  operating system operating system
File System operating system operating systemEthiopia Satlliet television
 
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 SystemsSam Bowne
 
File system interface
File system interfaceFile system interface
File system interfaceDayan Ahmed
 
File management in OS
File management in OSFile management in OS
File management in OSBhavik Vashi
 
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 SystemsSam Bowne
 

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 15 attributes
Module 15 attributesModule 15 attributes
Module 15 attributesPrem 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 11 : Inheritance
Module 11 : InheritanceModule 11 : Inheritance
Module 11 : InheritancePrem 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 2: Overview of c#
Module 2:  Overview of c#Module 2:  Overview of c#
Module 2: Overview of c#Prem 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
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloadingPrem Kumar Badri
 
C# Generic collections
C# Generic collectionsC# Generic collections
C# Generic collectionsPrem 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

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
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
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
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
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
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
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
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
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 

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