Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - File - Reading and writing to file using ASP.NET and VB.NET
Reading and writing to file using ASP.NET and VB.NET


ASP.NET File Tutorial

Read and write to file using ASP.NET (VB)

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Reading and writing to the file content using ASP.NET 2.0 and C# 2.0 is actually very simple.

First, you will need to import the System.IO namespace.

Imports System.IO;

The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support. Using the class of File ,and DirectoryInfo at here.

Use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.

Use the File class for typical operations such as creating, opening, deleting, appending, copying, moving, and renaming to files.

Use the ReadAllText method to opens a text file, reads all lines of the file, and then closes the file.

Use the WriteAllText method Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

We use the buttonDisplay_Click event to display the file content and use the buttonSave_Click even to save as a file.

The code as follows.

Protected Sub buttonDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim FilePath As String
FilePath = txtBoxInput.Text
If File.Exists(FilePath) Then
textBoxContents.Text = File.ReadAllText(FilePath)
Else
Response.Write("<script language='javascript'>window.alert('File not found');</script>")
Return
End If
End Sub

Protected Sub buttonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Folder As New DirectoryInfo(textboxPath.Text)
If Folder.Exists Then
If textboxName.Text <> String.Empty Then
Dim FilePathSave As String = Folder.ToString() + textboxName.Text File.WriteAllText(FilePathSave, textBoxContents.Text)
Else
Response.Write("<script language='javascript'>window.alert('Please enter file name');</script>")
End If
Else
Response.Write("<script language='javascript'>window.alert('Folder not found');</script>")
End If
End Sub

The front ReadWriteTextCSharp.aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td bgcolor="#eeeeee" class="header1">
<fieldset>
<legend>ReadWriteTextCsharp</legend>
<div>
<asp:Label ID="Label1" runat="server" Text="Enter path of file to be examined and click Display"></asp:Label><br />
<asp:TextBox ID="txtBoxInput" runat="server" Width="451px"></asp:TextBox>
<asp:Button ID="buttonDisplay" runat="server" Text="Display" OnClick="buttonDisplay_Click" /><br />
<fieldset>
<legend>Content of file</legend>
<asp:Label ID="Label2" runat="server" Text="You can edit the content and save as a file."></asp:Label><br />
<table>
<tr>
<td>
<asp:TextBox ID="textBoxContents" runat="server" tabIndex="0" height="200px" textMode="MultiLine" width="450px"></asp:TextBox>
</td>
</tr>
</table>
<fieldset>
<legend>Save as</legend>Path:
<asp:TextBox ID="textboxPath" runat="server" Width="237px"></asp:TextBox>File name:
<asp:TextBox ID="textboxName" runat="server" Width="93px"></asp:TextBox>
<asp:Button ID="buttonSave" runat="server" Text="Save As" Width="66px" OnClick="buttonSave_Click" /></fieldset>
</fieldset>
</div>
</fieldset>
</td>
</tr>
</table>

The flow for the code behind page is as follows.

Imports System.IO

Partial Class ReadWriteTextVB
Inherits System.Web.UI.Page
Protected Sub buttonDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim FilePath As String
FilePath = txtBoxInput.Text
If File.Exists(FilePath) Then
textBoxContents.Text = File.ReadAllText(FilePath)
Else
Response.Write("<script language='javascript'>window.alert('File not found');</script>")
Return
End If
End Sub

Protected Sub buttonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Folder As New DirectoryInfo(textboxPath.Text)
If Folder.Exists Then
If textboxName.Text <> String.Empty Then
Dim FilePathSave As String = Folder.ToString() + textboxName.Text File.WriteAllText(FilePathSave, textBoxContents.Text)
Else
Response.Write("<script language='javascript'>window.alert('Please enter file name');</script>")
End If
Else
Response.Write("<script language='javascript'>window.alert('Folder not found');</script>")
End If
End Sub
End Class


Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks