Monday, 25 July 2011

Working with Files and Folders using System.IO (VB.NET)


This tutorial will show you how to create, delete, move and set attribute to your specified directory and file using System.IO. You will see a sample we created in ASP.NET 2.0 and VB.NET.

First, import the namespace of System.IO.

Imports System.IO;

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

The System.IO namespace contains the Directory, DirectoryInfo and File Classes.

Directory Class exposes static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

DirectoryInfo Class exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

File Class Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.

Imports System.io
Partial Class _Default
Inherits System.Web.UI.Page
'show message from file
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
If File.Exists(Me.TextBox1.Text.Trim()) Then
Dim sr As StreamReader = New StreamReader(Me.TextBox1.Text.Trim())
Me.txtMsg.Text = sr.ReadToEnd()
sr.Close()
Else
Me.Response.Write("<script>alert('File does not Exist.')</script>")
End If
End Sub
'Write content of the txtMsg control to file
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If File.Exists(Me.TextBox1.Text.Trim()) Then
Dim sw As StreamWriter = New StreamWriter(Me.TextBox1.Text.Trim())
sw.WriteLine(Me.txtMsg.Text.Trim())
sw.Close()
Me.txtMsg.Text = ""
Else
Me.Response.Write("<script>alert('File does not Exist.')</script>")
End If
End Sub
'Create Directory
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click
If Directory.Exists(Me.txtDirectory.Text.Trim()) Then
Me.Response.Write("<script>alert('Directory Exist.')</script>")
Else
Directory.CreateDirectory(Me.txtDirectory.Text.Trim())
End If
End Sub
'Delete Directory
Protected Sub btnDelDirectory_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelDirectory.Click
If Directory.Exists(Me.txtDeleteDirectory.Text.Trim()) Then
Directory.Delete(Me.txtDeleteDirectory.Text.Trim())
Else
Me.Response.Write("<script>alert('Directory does not Exist.')</script>")
End If
End Sub
'Move Directory
Protected Sub btnMove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMove.Click
If Directory.Exists(Me.txtFrom.Text.Trim()) Then
If Directory.Exists(Me.txtTo.Text.Trim()) Then
Me.Response.Write("<script>alert('DestDirectory does not Exist.')</script>")
Else
Directory.Move(Me.txtFrom.Text.Trim(), Me.txtTo.Text.Trim())
End If
Else
Me.Response.Write("<script>alert('Directory does not Exist.')</script>") End If
End Sub
'Create file
Protected Sub btnCreateFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateFile.Click
If File.Exists(Me.txtCreateFile.Text.Trim()) Then
Me.Response.Write("<script>alert('File Exists.')</script>")
Else
Dim fs As FileStream = File.Create(Me.txtCreateFile.Text.Trim())
fs.Close()
End If
End Sub
'Delete File
Protected Sub btnDelFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelFile.Click
If File.Exists(Me.txtDelFile.Text.Trim()) Then
File.Delete(Me.txtDelFile.Text.Trim())
Else
Me.Response.Write("<script>alert('File does not Exist.')</script>")
End If
End Sub
'Move File
Protected Sub btnMoveFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFile.Click
If File.Exists(Me.txtFromFile.Text.Trim()) Then
If File.Exists(Me.txtTOFile.Text.Trim()) Then
Me.Response.Write("<script>alert('DestFile Exists.')</script>")
Else
File.Move(Me.txtFromFile.Text.Trim(), Me.txtTOFile.Text.Trim())
End If
Else
Me.Response.Write("<script>alert('File does not Exist.')</script>")
End If
End Sub
'Set File Attribute
Protected Sub btnFileSetAtb_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFileSetAtb.Click
If File.Exists(Me.txtFileAttribute.Text.Trim()) Then
If Me.rbFileReadOnly.Checked Then
File.SetAttributes(Me.txtFileAttribute.Text.Trim(), FileAttributes.ReadOnly)
ElseIf (Me.rbFileReadHide.Checked) Then
File.SetAttributes(Me.txtFileAttribute.Text.Trim(), FileAttributes.Hidden)
End If
Else
Me.Response.Write("<script>alert('File does not Exist.')</script>")
End If
End Sub
'Set Directory Attribute
Protected Sub btnAttribute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAttribute.Click
If (Directory.Exists(Me.txtDirAttribute.Text.Trim())) Then
Dim DirInfo As DirectoryInfo = New DirectoryInfo(Me.txtDirAttribute.Text.Trim())
If (Me.RbDirecotry.Checked) Then
DirInfo.Attributes = FileAttributes.ReadOnly
ElseIf (Me.RbDirecotry1.Checked) Then
DirInfo.Attributes = FileAttributes.Hidden
End If
Else
Me.Response.Write("<script>alert('Directory does not Exist.')</script>")
End If
End Sub
End Class

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

The front end Default.aspx page looks something like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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 runat="server">
<title>Default</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>Create Directory</legend>
Create Directory:<asp:TextBox ID="txtDirectory" runat="server" Width="181px">c:\\NewDirectory</asp:TextBox>
<asp:Button ID="btnCreate" runat="server" Text="Create Directory" OnClick="btnCreate_Click" />
<br />
Delete Directory:
<asp:TextBox ID="txtDeleteDirectory" runat="server" Width="178px">c:\\NewDirectory</asp:TextBox>
<asp:Button ID="btnDelDirectory" runat="server" Text="Delete Directory" OnClick="btnDelDirectory_Click" /><br />
Move Directory: From<asp:TextBox ID="txtFrom" runat="server">c:\\NewDirectory</asp:TextBox>TO<asp:TextBox ID="txtTo" runat="server">c:\\NewOtherDirectory</asp:TextBox>
<asp:Button ID="btnMove" runat="server" Text="Move Directory" Width="136px" OnClick="btnMove_Click" />
<br />
Set Directory Attribute:<asp:TextBox ID="txtDirAttribute" runat="server">c:\\NewDirectory</asp:TextBox>
&nbsp; &nbsp; &nbsp;<asp:RadioButton ID="RbDirecotry" runat="server" GroupName="Direcotry"
Text="ReadOnly" />
<asp:RadioButton ID="RbDirecotry1" runat="server" GroupName="Direcotry" Text="Hide" />&nbsp;
<asp:Button ID="btnAttribute" runat="server" Text="Set Attribute" OnClick="btnAttribute_Click" />
<br />
<legend style="width: 73px">Create File</legend>Create File:<asp:TextBox ID="txtCreateFile"
runat="server" Width="215px">c:\\NewDirectory\\NewFile.txt</asp:TextBox>&nbsp;<asp:Button ID="btnCreateFile" runat="server" OnClick="btnCreateFile_Click" Text="Create File" />
<br />
Delete File:<asp:TextBox ID="txtDelFile" runat="server" Width="217px">c:\\NewDirectory\\NewFile.txt</asp:TextBox>&nbsp;<asp:Button ID="btnDelFile" runat="server" OnClick="btnDelFile_Click" Text="Delete File" />
<br />
Move File: From<asp:TextBox ID="txtFromFile" runat="server" Width="206px">c:\\NewDirectory\\NewFile.txt</asp:TextBox>TO<asp:TextBox ID="txtTOFile" runat="server" Width="243px">c:\\NewFile.txt</asp:TextBox>
<asp:Button ID="btnMoveFile" runat="server" OnClick="btnMoveFile_Click" Text="Move File" />
<br />
Set File Attribute:<asp:TextBox ID="txtFileAttribute" runat="server" Width="291px">c:\\NewDirectory\\NewFile.txt</asp:TextBox>
&nbsp;<asp:RadioButton ID="rbFileReadOnly" runat="server" GroupName="File" Text="ReadOnly" />
<asp:RadioButton ID="rbFileReadHide" runat="server" GroupName="File" Text="Hide" />&nbsp;
<asp:Button ID="btnFileSetAtb" runat="server" OnClick="btnFileSetAtb_Click" Text="Set Attribute" />
<legend>Test File</legend>File Name:<asp:TextBox ID="TextBox1" runat="server" Width="253px">c:\\NewDirectory\\NewFile.txt</asp:TextBox>
<br />
<asp:TextBox ID="txtMsg" runat="server" Height="109px" TextMode="MultiLine" Width="342px"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Write to File" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Read From File" /><br />
</fieldset>
</div>
</form>
</body>
</html>

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


Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment