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 End Class '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 End SubDim sr As StreamReader = New StreamReader(Me.TextBox1.Text.Trim()) ElseMe.txtMsg.Text = sr.ReadToEnd() sr.Close() Me.Response.Write("<script>alert('File does not Exist.')</script>") End If'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 End SubDim sw As StreamWriter = New StreamWriter(Me.TextBox1.Text.Trim()) Elsesw.WriteLine(Me.txtMsg.Text.Trim()) sw.Close() Me.txtMsg.Text = "" Me.Response.Write("<script>alert('File does not Exist.')</script>") End If'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 End SubMe.Response.Write("<script>alert('Directory Exist.')</script>") ElseDirectory.CreateDirectory(Me.txtDirectory.Text.Trim()) End If'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 End SubDirectory.Delete(Me.txtDeleteDirectory.Text.Trim()) ElseMe.Response.Write("<script>alert('Directory does not Exist.')</script>") End If'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 End SubIf Directory.Exists(Me.txtTo.Text.Trim()) Then ElseMe.Response.Write("<script>alert('DestDirectory does not Exist.')</script>") ElseDirectory.Move(Me.txtFrom.Text.Trim(), Me.txtTo.Text.Trim()) End IfMe.Response.Write("<script>alert('Directory does not Exist.')</script>") End If '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 End SubMe.Response.Write("<script>alert('File Exists.')</script>") ElseDim fs As FileStream = File.Create(Me.txtCreateFile.Text.Trim()) End Iffs.Close() '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 End SubFile.Delete(Me.txtDelFile.Text.Trim()) ElseMe.Response.Write("<script>alert('File does not Exist.')</script>") End If'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 End SubIf File.Exists(Me.txtTOFile.Text.Trim()) Then ElseMe.Response.Write("<script>alert('DestFile Exists.')</script>") ElseFile.Move(Me.txtFromFile.Text.Trim(), Me.txtTOFile.Text.Trim()) End IfMe.Response.Write("<script>alert('File does not Exist.')</script>") End If'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 End SubIf Me.rbFileReadOnly.Checked Then ElseFile.SetAttributes(Me.txtFileAttribute.Text.Trim(), FileAttributes.ReadOnly) ElseIf (Me.rbFileReadHide.Checked) ThenFile.SetAttributes(Me.txtFileAttribute.Text.Trim(), FileAttributes.Hidden) End IfMe.Response.Write("<script>alert('File does not Exist.')</script>") End If'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 End SubDim DirInfo As DirectoryInfo = New DirectoryInfo(Me.txtDirAttribute.Text.Trim()) ElseIf (Me.RbDirecotry.Checked) Then DirInfo.Attributes = FileAttributes.ReadOnly ElseIf (Me.RbDirecotry1.Checked) ThenDirInfo.Attributes = FileAttributes.Hidden End IfMe.Response.Write("<script>alert('Directory does not Exist.')</script>") End If |
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> </div><legend>Create Directory</legend> </fieldset>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> <asp:RadioButton ID="RbDirecotry" runat="server" GroupName="Direcotry" Text="ReadOnly" /> <asp:RadioButton ID="RbDirecotry1" runat="server" GroupName="Direcotry" Text="Hide" /> <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> <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> <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> <asp:RadioButton ID="rbFileReadOnly" runat="server" GroupName="File" Text="ReadOnly" /> <asp:RadioButton ID="rbFileReadHide" runat="server" GroupName="File" Text="Hide" /> <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 /> </form> </body> </html> |
No comments:
Post a Comment