VisualBuilder
  Home > Csharp > Tutorials > Working with Files - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Working with Files
  << Prev: DNS [Domain Name System] Next: Generating Help File in C sharp >>

File access in .NET is generally handled with stream objects. In this section, we will discuss about the two classes BinaryWriter and BinaryReader to demonstrate the file handling in C#. The BinaryReader and BinaryWriter classes support binary file access. These classes permit binary operations to and from streams.


 


BinaryWriter


The BinaryWriter class allows primitive data types to be written to streams; and with the use of subclassing, you can override the methods of this class and fulfill your own unique character encoding requirements. The following table will summarize the properties of the class:-


 


 




































Name



Type



Purpose



BaseStream



Property



Allow access to binary writer underlying stream.



Close



Method



Close the Binary writer class and the underlying stream.



Flush



Method



Flushes out all data to the underlying stream and then clears buffers.



Seek



Method



Sets the current position within the current stream.



Write



Method



This overloaded method writes a value out to the current stream.



 


BinaryReader


The BinaryReader class, much like the BinaryWriter class, relies on a FileStream object to read files .


 


 



















































Method


 

Description



Close


 

Close the binary reader object and the base stream.



PeekChar


 

Reads the next available byte from the stream but does not advance the byte or character position within the file.



ReadBoolean


 

Reads a Boolean [True/False] value from the stream.



ReadByte


 

Reads a single byte from the stream. There is also a readbytes method for specifying the number of bytes to read.



ReadChar


 

Reads a single char value from the stream. There is also a readchars method for specifying the number of chars to read.



ReadInt16


 

Reads an integer value (2 bytes).



ReadInt32


 

Reads a long value (4 bytes).



ReadInt64


 

Reads an eight-byte signed integer.



 


Example: Demonstrate working with files


 


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


using System.IO;


 


namespace _CSharpApplication


{


public partial class Form4 : Form


{


 public Form4()


 {


  InitializeComponent();


 }


 


private void button1_Click( object sender, EventArgs e)


{ //Writing to the file


     FileStream myFStream = new FileStream ( "G:\\2CSharpApplication\\Test.txt" , FileMode         .OpenOrCreate, FileAccess .ReadWrite);


     BinaryWriter binWrit = new BinaryWriter (myFStream);


    string testString = "Writing into a File..This is a test Value." ;


  binWrit.Write(testString);


  binWrit.Close();


  myFStream.Close();


}


 


private void button2_Click( object sender, EventArgs e)


{ //Reading from the file.


    long imageWidth = 0;


    long imageHeight = 0;


    int imagePlanes = 0;


    int imageBitCount = 0;


    string [] cma = Environment .GetCommandLineArgs();


  if (cma.GetUpperBound(0) >= 1)


    {


        FileStream myFStream = new FileStream (cma[1], FileMode .Open, FileAccess .Read);


        BinaryReader binRead = new BinaryReader (myFStream);


    binRead.BaseStream.Position=0x12;


    imageWidth = binRead.ReadInt32();


    imageHeight= binRead.ReadInt32();


    imagesPlanes= binRead.ReadInt16();


    imagesBitCount = binRead.ReadInt16();


    Console .WriteLine( "[{0}] {1}x{2} {3}- bit" ,cma[1], imageWidth, imageHeight, imageBitCount);


    binRead.Close();


    myFStream.Close();


    }


}


private void button3_Click( object sender, EventArgs e)


{ //Copying File.


    string [] strcla = Environment .GetCommandLineArgs();


  if (strcla.GetUpperBound(0) == 2)


    {


        FileInfo cpyfi = new FileInfo (strcla[1]);


    cpyfi.CopyTo(strcla[2], true );


    MessageBox .Show( "Copied " + cpyfi.Length + " bytes." );


    }


  else


    MessageBox .Show( "Usage: cp <input file> <output file>" );


}


 


private void button4_Click( object sender, EventArgs e)


{ //Deleting File.


    string [] strcla = Environment .GetCommandLineArgs();


  if (strcla.GetUpperBound(0) == 1)


    {


        FileInfo delfi = new FileInfo (strcla[1]);


    delfi.Delete();


    MessageBox .Show( "File : " + strcla[1]);


    MessageBox .Show( "Attributes: " + delfi.Attributes.ToString());


    MessageBox .Show( "File Deleted..." );


    }


  else


    MessageBox .Show( "Usage: rm <filename>" );


}


 


private void button5_Click( object sender, EventArgs e)


{ //Moving File.


    string [] strcla = Environment .GetCommandLineArgs();


  if (strcla.GetUpperBound(0) == 2)


    {


        FileInfo movfi = new FileInfo (strcla[1]);


    movfi.MoveTo(strcla[2]);


    MessageBox .Show( "File Created : " +movfi.CreationTime.ToString());


    MessageBox .Show( "Moved to : " + strcla[2]);


    }


  else


    MessageBox .Show( "Usage: mv <source file> <destination file>" );


  }


 }


}


  << Prev: DNS [Domain Name System] Next: Generating Help File in C sharp >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
Exchange Hosting
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising