Working with Files and Directories using ASP.NET
page 2 of 8
by SANJIT SIL
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 63283/ 105

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");
else
Response.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.

GetFileSystemEntries Method

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

Listing 11

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

The GetFileSystemEntries 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 and file that matches the search pattern of a given directory.

Listing 12

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

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

GetLastAcessTime Method

GetLastAccessTime returns the last access date and time of the file or directory as specified in the path.

Listing 13

string cd=Directory.GetLastAccessTime(@"E:\TestDirectoryFile").ToString();

GetLastWriteTime Method

GetLastWriteTime returns the last modified date and time of the file or directory as specified in the path.

Listing 14

string cd=Directory.GetLastWriteTime(@"E:\TestDirectoryFile").ToString();

GetLogicalDrives Method

GetLogicalDrives Method returns a string array that contains a list of the available drives.

Listing 15

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

GetParent Method

GetParent Method returns the parent directory of the specified path.

Listing 16

string cd=Directory.GetParent("E:\\TestDirectoryFile").ToString();

SetCreationTime Method

SetCreationTime Method sets the date and time of the file or directory as specified in the path.

Listing 17

Directory.SetCreationTime("E:\\TestDirectoryFile1",System.DateTime.Now);

SetCurrentDirectory Method

SetCurrentDirectory Method sets the application’s current working directory to the directory as specified in the path.

Listing 18

Directory.SetCurrentDirectory(@"E:\TestDirectoryFile");

SetLastAccessTime Method

SetLastAccessTime Method sets the last access date and time of the file or directory as specified in the path.

Listing 19

Directory.SetLastAccessTime("E:\\TestDirectoryFile",System.DateTime.Now);

SetLastWriteTime Method

SetLastWriteTime Method sets the last written date and time of the directory as specified in the path.

Listing 20

Directory.SetLastWriteTime(("E:\\TestDirectoryFile1",System.DateTime.Now); 

Move Method

Move Method moves a file or a directory and its contents to a new location.

Listing 21

Directory.Move("E:\\TestDirectoryFile1","E:\\TestDirectoryFile2");

DirectoryInfo

The DirectoryInfo class is similar to the Directory class, but contains only static methods.  A security check is performed only when the instance is created.  The methods of DirectoryInfo class have been explained in the sections that follow.

Create Method

Create Method creates a directory.

Listing 22

DirectoryInfo di=new DirectoryInfo(@"E:\TestDirectoryFile");
di.Create();  

CreateSubdirectory Method

CreateSubDirectory Method creates a subdirectory.

Listing 23

di.CreateSubdirectory(“TestSubDirectory”);

Delete Method

Delete Method deletes the directory.  This method is overloaded.

Listing 24

di.Delete();  // use when directory is empty;

di.Delete(true); // use to delete all files and directories in the directory.

GetDirectories Method

GetDirectories Method returns the subdirectories of the given directory.

Listing 25

DirectoryInfo df=new DirectoryInfo(Server.MapPath("Storage"));                      
DirectoryInfo[] abc=df.GetDirectories();
for(int i=0;i<abc.Length;i++)                                 
{
Response.Write(abc[i]);
Response.Write("<BR>");
}

GetDirectories Method is overloaded.  We can pass the search pattern as a string in the form of an optional parameter, which will return a directory list containing the name of each directory that matches the search pattern.

Listing 26

DirectoryInfo df=new DirectoryInfo(Server.MapPath("Storage"));
DirectoryInfo[] abc=df.GetDirectories("D*");
for(int i=0;i<abc.Length;i++)                                 
{
Response.Write(abc[i]);
Response.Write("<BR>");
}

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

GetFiles Method

The GetFiles Method returns a file list from the current directory.

Listing 27

DirectoryInfo df=new DirectoryInfo(Server.MapPath("Storage"));
FileInfo[] abc=df.GetFiles();
for(int i=0;i<abc.Length;i++)
{
Response.Write(abc[i]);
Response.Write("<BR>");
}

The GetFiles Method is overloaded.  We can pass the search pattern as a string in form of an optional parameter, which will return a file list containing the name of each file that matches the search pattern of a given directory.

Listing 28

DirectoryInfo df=new DirectoryInfo(Server.MapPath("Storage"));
FileInfo[] abc=df.GetFiles("x*");
for(int i=0;i<abc.Length;i++)
{
Response.Write(abc[i]);
Response.Write("<BR>");
}

In the above code listing all files will be displayed starting with the letter x.

GetFileSystemInfos Method

 GetFileSystemInfos Method gets a collection of all the files and directories inside a folder.

Listing 29

DirectoryInfo di=new DirectoryInfo(@"E:\TestDirectoryFile");
FileSystemInfo[] abc=di.GetFileSystemInfos();
for(int i=0;i<abc.Length;i++)
{
Response.Write(abc[i]);
Response.Write("<BR>");
}

The GetFileSystemInfos Method is overloaded.  We can pass a search pattern as a string in the form of an optional parameter, which will return the directory and file list containing the name of each file and directory that matches the search pattern.

Listing 30

DirectoryInfo di=new DirectoryInfo(@"E:\TestDirectoryFile ");
FileSystemInfo[] abc=di.GetFileSystemInfos("D*");
for(int i=0;i<abc.Length;i++)
{
Response.Write(abc[i]);
Response.Write("<BR>");
}

In the above code listing all directories and files will be displayed starting with the letter D.

MoveTo Method

MoveTo Method moves a DirectoryInfo instance and its contents to a new path.

Listing 31

di.MoveTo(@"E:\TestDirectoryFile3");

View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2022 ASPAlliance.com  |  Page Processed at 2022-10-06 4:26:07 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search