|
Besides File, you can use the StreamWriter class to create a file. To do this, declare a variable of type StreamWriter and initialize it using one of its constructors.
One of the most routine operations performed on a class consists of writing information to it. And one of the most useful classes in this domain is called StreamWriter. The StreamWriter class is derived from the TextWriter class. To create a file using the StreamWriter class, you can declare a StreamWriter variable and initialize it using one of its constructors. After creating the file, you can write information to it by calling the Write() or the WriteLine() method. Make sure you close the stream after using it. Also make sure you use exception handling in your code. |
|
|
Here is an example: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
StreamWriter stmWrite =
new StreamWriter(Server.MapPath(txtFileSave.Text + "mpl"));
stmWrite.WriteLine(txtEmployeeNumber.Text);
stmWrite.WriteLine(txtDateHired.Text);
stmWrite.WriteLine(txtFirstName.Text);
stmWrite.WriteLine(txtLastName.Text);
stmWrite.WriteLine(ddlGenders.Text);
stmWrite.WriteLine(txtHourlySalary.Text);
stmWrite.Close();
txtEmployeeNumber.Text = "";
txtDateHired.Text = DateTime.Today.ToString();
txtFirstName.Text = "";
txtLastName.Text = "";
ddlGenders.Text = "Unknown";
txtHourlySalary.Text = "0.00";
txtFileSave.Text = "";
txtFileOpen.Text = "";
}
}
Besides StreamWriter, to create a file and write information to it, you can use the BinaryWriter class. You start by declaring a BinaryWriter variable and initialize it using one of its constructors, passing a Stream-based object.
Before exploring the contents of a file, you must first open it. To open a file using the File class, you can call its Open method that is overloaded with three versions. If the information in the file is raw text, you can call the OpenText() method. After opening a file, you can read its content. To support the ability to read from a file, you can use the StreamReader class that is derived from the TextReader class. To use it, declare a variable of type StreamReader and use one of its constructors to specify the name of, or the path to, the file. To read information from the file, you can call its Read() or its ReadLine() method. Here is an example: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
StreamWriter stmWrite =
new StreamWriter(Server.MapPath(txtFileSave.Text + "mpl"));
stmWrite.WriteLine(txtEmployeeNumber.Text);
stmWrite.WriteLine(txtDateHired.Text);
stmWrite.WriteLine(txtFirstName.Text);
stmWrite.WriteLine(txtLastName.Text);
stmWrite.WriteLine(ddlGenders.Text);
stmWrite.WriteLine(txtHourlySalary.Text);
stmWrite.Close();
txtEmployeeNumber.Text = "";
txtDateHired.Text = DateTime.Today.ToString();
txtFirstName.Text = "";
txtLastName.Text = "";
ddlGenders.Text = "Unknown";
txtHourlySalary.Text = "0.00";
txtFileSave.Text = "";
txtFileOpen.Text = "";
}
protected void btnOpen_Click(object sender, EventArgs e)
{
using (StreamReader stmReader =
new StreamReader(Server.MapPath(txtFileOpen.Text + "mpl")))
{
txtEmployeeNumber.Text = stmReader.ReadLine();
txtDateHired.Text = stmReader.ReadLine();
txtFirstName.Text = stmReader.ReadLine();
txtLastName.Text = stmReader.ReadLine();
ddlGenders.Text = stmReader.ReadLine();
txtHourlySalary.Text = stmReader.ReadLine();
}
}
}
Instead of StreamReader, you can use the BinaryReader class to read information from a file. |
|
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="index.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exercise</title>
</head>
<body>
<h3>Employees Records</h3>
<form id="frmExercise" runat="server">
<table style="width:500px;">
<tr>
<td style="width:100px;">Employee #:</td>
<td style="width:100px;">
<asp:TextBox ID="txtEmployeeNumber" runat="server" Width="120px"></asp:TextBox>
</td>
<td> </td>
<td>Date Hired:</td>
<td>
<asp:TextBox ID="txtDateHired" runat="server" Width="120px"></asp:TextBox>
</td>
</tr>
<tr>
<td>First Name:</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server" Width="120px"></asp:TextBox>
</td>
<td> </td>
<td>Last Name:</td>
<td>
<asp:TextBox ID="txtLastName" runat="server" Width="120px"></asp:TextBox>
</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<asp:DropDownList ID="ddlGenders" runat="server" Width="120px">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
<asp:ListItem>Unknown</asp:ListItem>
</asp:DropDownList>
</td>
<td> </td>
<td>Hourly Salary:</td>
<td>
<asp:TextBox ID="txtHourlySalary" runat="server" Width="120px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="5"><hr /></td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" Width="97px" />
</td>
<td style="width:100px;">
<asp:TextBox ID="txtFileSave" runat="server" Width="120px"></asp:TextBox>
</td>
<td style="width:100px;">
</td>
<td style="width:100px;">
<asp:Button ID="btnOpen" runat="server" Text="Open" Width="97px" />
</td>
<td>
<asp:TextBox ID="txtFileOpen" runat="server" Width="120px"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</html>