CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Tuesday, September 12, 2006

50 Examples for Working with Files and Directories using ASP.NET

Listing 11 to 50 for Working with Files and Directories using ASP.NET

Managing Directories

There are two classes to manage directories, Directory and DirectoryInfo.

Directory

The Directory class is responsible for creating, moving and enumerating directories and subdirectories.  All of its methods are static and require a security check for each call.  The security check verifies the permission of the current user to perform the specified operation.  The methods of Directory class have been explained in the sections that follow.

CreateDirectory Method

CreateDirectory Method creates a directory if one does not exist.

Listing 1

Directory.CreateDirectory(@"E:\TestDirectory");

Delete Method

Delete method deletes a directory.

Listing 2

Directory.Delete(@"E:\TestDirectory");

Exists Method

Exists Method determines the existence of a given directory.

Listing 3

Directory.CreateDirectory(@"E:\TestDirectory");if(Directory.Exists(@"E:\TestDirectory"))Response.Write("Directory Exist");elseResponse.Write("Directory does not exist");

GetCreationTime Method

GetCreationTime Method displays the date and time of creation of a given directory.

Listing 4

Response.Write(Directory.GetCreationTime(@"E:\TestDirectory"));

GetCurrentDirectory Method

GetCurrentDirectory Method returns the path of current working directory of the application.

Listing 5

String cd=Directory.GetCurrentDirectory();

GetDirectories Method

GetDirectories Method returns a string array filled with the fully qualified names of each contained directory of a given directory.

Listing 6

string[] abc=Directory.GetDirectories(Server.MapPath("Repository"));foreachstring a in abc)                                   {Response.Write(a);Response.Write("<BR>");}

The GetDirectories Method is overloaded.  We can pass the search pattern as a string in the form of a second optional parameter, which will return a string array filled with the fully qualified names of each contained directory that matches the search pattern of a given directory.

Listing 7

string[] abc=Directory.GetDirectories(Server.MapPath("Repository"),"b*");foreachstring a in abc)                           {Response.Write(a);Response.Write("<BR>");}

In the above code listing all directories will be displayed starting with the letter b.

GetDirectoryRoot Method

GetDirectoryRoot Method returns root information after taking a path as an argument.

Listing 8

cd=Directory.GetDirectoryRoot(@"E:\TestDirectoryFile");

GetFiles Method

GetFiles Method returns a string array filled with the fully qualified names of each contained file of a given directory.

Listing 9

string[] abc=Directory.GetFiles(Server.MapPath("Repository"));foreachstring a in abc)                                                                      {Response.Write(a);Response.Write("<BR>");}

The GetFiles Method is overloaded.  We can pass the search pattern as a string in the form of a second optional parameter, which will return a string array filled with the fully qualified names of each contained file that matches the search pattern of a given directory.

Listing 10

string[] abc=Directory.GetFiles(Server.MapPath("Repository"),"*.aspx");foreachstring a in abc)                                            {Response.Write(a);Response.Write("<BR>");}

In the above code listing all files having extension aspx will be displayed.

Listing 11 to 50 for Working with Files and Directories using ASP.NET

Post a Comment