Monday, 25 July 2011

Working with Files and Folders using System.IO (C#)


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 C#.

First, import the namespace of System.IO.

using System.IO;

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;

public class MessageBox
{
private System.Web.UI.Page p;
public MessageBox(System.Web.UI.Page page)
{
p = page;
}
public void Show(string Message)
{
string script = "<script>alert('"+Message+"')</script>";
p.Response.Write(script);
}
}
public partial class _Default : System.Web.UI.Page
{
MessageBox MB = null;
protected void Page_Load(object sender, EventArgs e)
{
MB = new MessageBox(this);
}
/// <summary>
/// Create Directory
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnCreate_Click(object sender, EventArgs e)
{
if (Directory.Exists(this.txtDirectory.Text.Trim()))
{
MB.Show("Directory Exists.");
}
else
{

Directory.CreateDirectory(this.txtDirectory.Text.Trim());
}
}
/// <summary>
/// Delete Directory
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelDirectory_Click(object sender, EventArgs e)
{
if (Directory.Exists(this.txtDeleteDirectory.Text.Trim()))
{
Directory.Delete(this.txtDeleteDirectory.Text.Trim());
}
else
{
MB.Show("Directory does Exist.");
}
}
/// <summary>
/// Move Directory
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMove_Click(object sender, EventArgs e)
{
if (Directory.Exists(this.txtFrom.Text.Trim()))
{
if (Directory.Exists(this.txtTo.Text.Trim()))
{
MB.Show("destDirectory Exists.");
}
else
{
Directory.Move(this.txtFrom.Text.Trim(), this.txtTo.Text.Trim());
}
}
else
{
MB.Show("Directory does not Exist.");
}
}
/// <summary>
/// Set Directory Attribute
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAttribute_Click(object sender, EventArgs e)
{
if (Directory.Exists(this.txtDirAttribute.Text.Trim()))
{
DirectoryInfo DirInfo = new DirectoryInfo(this.txtDirAttribute.Text.Trim());
if (this.RbDirecotry.Checked)
{
DirInfo.Attributes = FileAttributes.ReadOnly;
}
else if (this.RbDirecotry1.Checked)
{
DirInfo.Attributes = FileAttributes.Hidden;
}
}
else
{
MB.Show("Directory does not Exist.");
}

}
/// <summary>
/// Create File
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnCreateFile_Click(object sender, EventArgs e)
{
if (File.Exists(this.txtCreateFile.Text.Trim()))
{
MB.Show("File Exists.");
}
else
{
FileStream fs = File.Create(this.txtCreateFile.Text.Trim());
fs.Close();
}
}
/// <summary>
/// Delete File
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelFile_Click(object sender, EventArgs e)
{
if (File.Exists(this.txtDelFile.Text.Trim()))
{
File.Delete(this.txtDelFile.Text.Trim());
}
else
{
MB.Show("File does not Exist.");
}
}
/// <summary>
/// Move File
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMoveFile_Click(object sender, EventArgs e)
{
if (File.Exists(this.txtFromFile.Text.Trim()))
{
if (File.Exists(this.txtTOFile.Text.Trim()))
{
MB.Show("DestFile Exists.");
}
else
{
File.Move(this.txtFromFile.Text.Trim(), this.txtTOFile.Text.Trim());
}
}
else
{
MB.Show("File does not Exist.");
}
}
/// <summary>
/// Set File Attribute
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnFileSetAtb_Click(object sender, EventArgs e)
{
if (File.Exists(this.txtFileAttribute.Text.Trim()))
{

if (this.rbFileReadOnly.Checked)
{
File.SetAttributes(this.txtFileAttribute.Text.Trim(), FileAttributes.ReadOnly);
}
else if (this.rbFileReadHide.Checked)
{
File.SetAttributes(this.txtFileAttribute.Text.Trim(), FileAttributes.Hidden);
}
}
else
{
MB.Show("File does not Exist.");
}
}
/// <summary>
/// Show message from file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
if (File.Exists(this.TextBox1.Text.Trim()))
{
StreamReader sr = new StreamReader(this.TextBox1.Text.Trim());
this.txtMsg.Text = sr.ReadToEnd();
sr.Close();
}
else
{
MB.Show("File does not exist.");
}
}
/// <summary>
/// Write content of the control txtMsg to File.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
if (File.Exists(this.TextBox1.Text.Trim()))
{
StreamWriter sw = new StreamWriter(this.TextBox1.Text.Trim());
sw.WriteLine(this.txtMsg.Text.Trim());
sw.Close();
this.txtMsg.Text = "";
}
else
{
MB.Show("File does not exist.");
}
}
}

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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 VB.NET 2005 Version? Click Here!


Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment