Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to read and write binary files in ASP.NET

Author: Rachel Nicole
Download Source Code : 1352_ReadWriteBinaryFile.zip

BinaryReader and BinaryWriter classes are available in ASP.NET to read data from binary file and to write data into binary file respectively.

 
Binary files are great to store your data but this data is not human readable. ASP.NET provides BinaryReader and BinaryWriter classes to work with binary files.

FileStream class supports both synchronous and asynchronous read and write operations. The FileStream class instance is provided to BinaryReader and BinaryWriter classes to read and write binary data from the specified file with specified file operation. BinaryWriter class has Write() method which has 18 overloads available to write string, integer, byte and other types of data into binary file. BinaryReader class has all methods available to read string, integer, byte and other types of data from binary file.

 

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual Basic or Visual C#  > ASP.NET Empty Web Site
  4. Right click on web site > Add New Item > Web Form
  5. Write code below in Default.aspx page

    <table>
        <tr>
            <td>Write any text data:</td>
            <td>
               <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Write any integer data:</td>
            <td>
               <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
               <asp:Button ID="Button1" runat="server" Width="200px"
                   Text="Click to write into Binary File" onclick="Button1_Click" />
             </td>
         </tr>
         <tr>
             <td colspan="2">
                <asp:Button ID="Button2" runat="server" Width="200px"
                    Text="Click to read from Binary File" onclick="Button2_Click" />
             </td>
         </tr>
    </table>
    <asp:Label ID="Label1" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="Label3" runat="server" ></asp:Label>
     
  6. Write code below in first Button click event in code behind file. You have to import or use “System.IO” namespace as FileStream, StreamReader and StreamWriter classes are available in System.IO namespace.

    C#
     
    using System.IO;
     
    protected void Button1_Click(object sender, EventArgs e) {
        FileStream fStream = null;
        BinaryWriter bWriter = null;
        try {
            string filePath = Server.MapPath("~/Test.bin");
            fStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
            bWriter = new BinaryWriter(fStream);
            string txt = TextBox1.Text;
            int num = Convert.ToInt32(TextBox2.Text);
            bWriter.Write(txt);
            bWriter.Write(num);
            bWriter.Flush();
     
            Label3.Text = "Binary data is written";
        }
        catch (Exception ex) {
            Label3.Text = ex.Message;
        }
        finally {
            bWriter.Close();
            fStream.Close();
        }
    }
     
    VB.NET

    Imports System.IO
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim fStream As FileStream = Nothing
        Dim bWriter As BinaryWriter = Nothing
        Try
            Dim filePath As String = Server.MapPath("~/Test.bin")
            fStream = New FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)
            bWriter = New BinaryWriter(fStream)
            Dim txt As String = TextBox1.Text
            Dim num As Integer = Convert.ToInt32(TextBox2.Text)
            bWriter.Write(txt)
            bWriter.Write(num)
            bWriter.Flush()
     
            Label3.Text = "Binary data is written"
        Catch ex As Exception
            Label3.Text = ex.Message
        Finally
            bWriter.Close()
            fStream.Close()
        End Try
    End Sub
     
    I have created a FileStream object and a BinaryWriter object and in try block I have provided file path in the constructor of FileStream class. I have also provided creation mode and read write permission in the constructor of FileStream class. FileStream object is provided in the constructor of BinaryWriter class. I have called the Write() method of BinaryWriter class two times for text data and for numeric data.
     
  7. Write code below in second button click event

    C#
     
    protected void Button2_Click(object sender, EventArgs e) {
        FileStream fStream = null;
        BinaryReader bReader = null;
        try {
            string filePath = Server.MapPath("~/Test.bin");
            fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            bReader = new BinaryReader(fStream);
            string txt = TextBox1.Text;
            int num = Convert.ToInt32(TextBox2.Text);
     
            Label1.Text = bReader.ReadString();
            Label2.Text = bReader.ReadInt32().ToString();
     
            Label3.Text = "";
        }
        catch (Exception ex) {
            Label3.Text = ex.Message;
        }
        finally {
            bReader.Close();
            fStream.Close();
        }
    }
     
    VB.NET
     
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        Dim fStream As FileStream = Nothing
        Dim bReader As BinaryReader = Nothing
        Try
            Dim filePath As String = Server.MapPath("~/Test.bin")
            fStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
            bReader = New BinaryReader(fStream)
            Dim txt As String = TextBox1.Text
            Dim num As Integer = Convert.ToInt32(TextBox2.Text)
     
            Label1.Text = bReader.ReadString()
            Label2.Text = bReader.ReadInt32().ToString()
     
            Label3.Text = ""
        Catch ex As Exception
            Label3.Text = ex.Message
        Finally
            bReader.Close()
            fStream.Close()
        End Try
    End Sub
     
    I have created FileStream object and BinaryReader object. This time I have provided file open mode and read permission in FileStream constructor along with file path. FileStream object is provided to BinaryReader class constructor this time. I have called ReadString() method of BinaryReader class to read string data and ReadInt32() method to read integer data from binary file.
     
  8. Build website and view in browser.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to insert record into database from GridView control in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net