Slapping a Photo Gallery Together in ASP.NET Part I - Etcetera
(Page 5 of 6 )
So far, I've demonstrated how you can use different .NET objects to navigate around the file system. For the sake of completeness, I will now demonstrate how you can create folders using the DirectoryInfo() object.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<SCRIPT Language="C#" runat=server>
void Page_Load(Object o,EventArgs a) {
if(Page.IsPostBack) {
// Clear the output text box
output.Text = "";
try {
if(txtDirectoryName.Text != "") {
// path to the root folder
string strFileSystemPath = "E:\\inetpub\\wwwroot\\Gallery";
// create an instance of the DirectoryInfo() object
// on the basis of the file-system path provided
DirectoryInfo objCurrentFolder = new DirectoryInfo(strFileSystemPath);
// A quick check if a folder by the same name already exists
string strNewFolderPath = strFileSystemPath + "\\" + txtDirectoryName.Text;
DirectoryInfo objNewFolder = new DirectoryInfo(strNewFolderPath);
// check if folder exists already
if(objNewFolder.Exists) {
// if it does, display error message...
output.Text = "A folder by this name already exists.";
} else {
// ... and if it does, create a new folder with the required name
objNewFolder = objCurrentFolder.CreateSubdirectory(txtDirectoryName.Text);
output.Text = "Folder \"" + txtDirectoryName.Text + "\" was created successfully.";
}
} else {
output.Text = "Please enter a folder name to be created.";
}
} catch (Exception e) {
output.Text = "An error occurred: " + e.Message;
} finally {
hypBack.Visible = true;
txtDirectoryName.Visible = false;
btnSubmit.Visible = false;
}
}
}
</SCRIPT>
<HTML>
<HEAD>
<BASEFONT face="Arial">
</HEAD>
<BODY>
<FORM runat="server">
<asp:Label id="output" runat="server" Text="Folder Name"/>
<asp:TextBox id="txtDirectoryName" Text="" runat="server"/>
<asp:Button id="btnSubmit" Text="Create Directory" runat="server" />
</FORM>
<asp:HyperLink id="hypBack" Text="<< Back" NavigateUrl="javascript:window.history.go(-1)" visible="false" runat="server" />
</BODY>
</HTML>
Here is the output after the required folder has been successfully created:

If things do not go quite the way you expected, you should an error that looks like this:.

To kick things off in this script, I've created an instance DirectoryInfo() object for the root folder, specified in the "strFileSystemPath" string variable. Next, I've instantiated another DirectoryInfo() object representing the new folder that I wish to create.
The "Exists" property of the DirectoryInfo() object can be used to verify whether a folder with the same name already exists. If yes, the script exits with a polite message, as seen above. If not, I've invoked the CreateSubdirectory() method to create the required folder.
The many "if-else" blocks in the script take care of different scenarios that I may come across. A closer look at the error messages associated with each block should help to make things clear.
Deleting a directory is not a problem. The DirectoryInfo() object has a handy Delete() method to delete a particular directory. Go ahead, give it a shot and I'm sure that you'll not be disappointed.
Want to make things interesting? Just pass a Boolean "true" value to this Delete() method. Within seconds, the Delete() method will wipe out your entire collection. As you might have guessed, if this Boolean parameter is set to "true," the ASP.NET script will delete all files recursively.
This is not a very smart move. Always use the Delete() method with great care. Period.
Next: Conclusion >>
More ASP.NET Articles
More By Harish Kamath