Read/write binary file (C#) : Binary File « File Directory « ASP.Net






Read/write binary file (C#)

<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
    BinaryWriter objBinaryWriter;
    BinaryReader objBinaryReader;
    
    private void WriteButton_Click(object Sender, EventArgs e)
    {
       try
       {
           FileStream objFileStream;
    
           objFileStream = new FileStream("myFileCS.bin", FileMode.Create);
           objBinaryWriter = new BinaryWriter(objFileStream);
    
           string Name = NameTextBox.Text;
           int Age = Int32.Parse(AgeTextBox.Text);
           char Sex = Char.Parse(SexTextBox.Text);
    
           objBinaryWriter.Write(Name);
           objBinaryWriter.Write(Age);
           objBinaryWriter.Write(Sex);
       }
       catch (FileNotFoundException FileEx)
       {
           MessageLabel.Text = FileEx.Message;
           return;
       }
       catch (Exception Ex)
       {
           MessageLabel.Text = Ex.Message;
           return;
       }

       WriteBinaryPanel.Visible = false;
       ReadBinaryPanel.Visible = true;
    }
    
    private void ReadButton_Click(object Sender, EventArgs e)
    {
        FileStream objFileStream;
    
        try
        {
            objFileStream = new FileStream("myFileCS.bin", FileMode.Open);
    
            objBinaryReader = new BinaryReader(objFileStream);
    
            NameLabel.Text = "Your Name: " + objBinaryReader.ReadString();
            AgeLabel.Text = "Your Age: " + objBinaryReader.ReadInt32();
            SexLabel.Text = "Your Sex: " + objBinaryReader.ReadChar();
        }
        catch (FileNotFoundException FileEx)
        {
            MessageLabel.Text = FileEx.Message;
            return;
        }
        catch (Exception Ex)
        {
            MessageLabel.Text = Ex.Message;
            return;
        }

    }

</script>
<html>
  <head>
  </head>
  <body>
    <form runat="server">
      <p>
        <asp:Panel id="WriteBinaryPanel" Runat="Server">
          <p>
            Please enter your Name:
            <asp:TextBox id="NameTextBox" runat="server"></asp:TextBox>
          </p>
          <p>
            Please enter your Age:
            <asp:TextBox id="AgeTextBox" runat="server"></asp:TextBox>
          </p>
          <p>
            Please enter your Sex:
            <asp:TextBox id="SexTextBox" 
                         runat="Server" 
                         MaxLength="1" 
                         Columns="1"></asp:TextBox>
            (M/F)
          </p>
          <p>
            <asp:Button id="WriteButton" 
                        onclick="WriteButton_Click" 
                        runat="server" 
                        Text="Write It!"></asp:Button>
          </p>
        </asp:Panel>
        <asp:Panel id="ReadBinaryPanel" Runat="Server" >
          <p>
            <asp:Button id="ReadButton" 
                        onclick="ReadButton_Click" 
                        runat="server" 
                        Text="Read It!"></asp:Button>
          </p>
          <p>
            <asp:Label id="NameLabel" Runat="Server"></asp:Label>
          </p>
          <p>
            <asp:Label id="AgeLabel" Runat="Server"></asp:Label>
          </p>
          <p>
            <asp:Label id="SexLabel" Runat="Server"></asp:Label>
          </p>
        </asp:Panel>
      </p>
      <p>
        <asp:Label id="MessageLabel" 
                   runat="server" 
                   EnableViewState="False"></asp:Label>
      </p>
    </form>
  </body>
</html>

 








Related examples in the same category

1.Read/write binary file (VB)