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

Managing Files

To manage files we have File class and FileInfo class.

File

The File Class provides static methods for the creation, copying, deletion, moving and opening of files.  It also aids in the creation of FileStream objects.  The methods of the File class have been explained in the sections that follow.

AppendText Method

AppendText Method appends text in an existing file as specified in the path.  This method returns a streamwriter object that is used to append contents in the file.

Listing 32

StreamWriter sw=File.AppendText(@"E:\test.txt");
sw.WriteLine("This is line no 4" );
sw.WriteLine("This is line no 5");
sw.WriteLine("This is line no 6");
sw.Close();

Copy Method

Copy Method copies an existing file to a new file.

Listing 33

File.Copy(Server.MapPath("TestDirectoryFile1.doc"),Server.MapPath("TestDirectoryFile2.doc"));

Create Method

Create Method creates a file in the specified path.

Listing 34

File.Create(Server.MapPath("TestFile.doc"));

CreateText Method

CreateText Method opens or creates a new file as specified in the path.  This method returns a streamwriter object that is used to write content in the file.  Contents of the existing file are overwritten provided the file is not in read only mode.

Listing 35

StreamWriter sw=File.CreateText(@"E:\test.txt");
sw.WriteLine("This is line no 1" );
sw.WriteLine("This is line no 2");
sw.WriteLine("This is line no 3");
sw.Close();

Delete Method

Delete Method deletes the specified file.

Listing 36

File.Delete(Server.MapPath("TestFile.doc"));

Exists Method

Exists Method determines whether the specified file exists.

Listing 37

if(File.Exists(Server.MapPath("TestDirectoryFile1.doc")))
{                 
Response.Write("File Exist");
}
else
{
Response.Write("File Does Not Exist");
      
}     

GetCreationTime Method

GetCreationTime Method returns the creation date and time of the file specified in the path.

Listing 38

string str=File.GetCreationTime(@"E:\test.txt").ToString();

GetLastAccessTime Method

GetLastAccessTime Method returns the last access date and time of the specified file in the path.

Listing 39

string str=File.GetLastAccessTime(@"E:\test.txt").ToString();

GetLastWriteTime Method

GetLastWriteTime Method returns the last written date and time in the specified file in the path.

Listing 40

string str=File.GetLastWriteTime(@"E:\test.txt").ToString();

Move Method

Move Method moves a specified file to a new location.

Listing 41

File.Move(Server.MapPath("TestDirectoryFile1.doc"),Server.MapPath("TestDirectoryFile2.doc"));

Open Method

Open Method opens a FileStream on the specified path with read or write access

The syntax is:

File.Open(Server.MapPath("TestDirectoryFile.doc"), FileMode, FileAccess, FileShare);

Once a file is opened and we have FileStream object, we can create a reader or writer object to work with the file’s contents.

The following code listing shows how to write a few lines of text to a file using the StreamWriter Class.

Listing 42

FileStream fs;
StreamWriter sw;
fs=File.Open(@"E:\test.txt",FileMode.Append,FileAccess.Write,FileShare.Write);
sw=new StreamWriter(fs);
sw.WriteLine("Write 1");
sw.WriteLine("Write 2");
sw.WriteLine("Write 3");
sw.Close();

FileInfo

The FileInfo class provides instance methods for the creation, copying, deletion, moving and opening of files.  It also provides members to create FileStream objects.

AppendText Method

AppendText Method appends text in an existing file using StreamWriter object returned by this method.

Listing 43

FileInfo fi=new FileInfo(@"E:\test.txt");
StreamWriter sw=fi.AppendText();
sw.WriteLine("This is a new line");
sw.Close();

CopyTo Method

CopyTo Method copies an existing file to a new file.

Listing XXXXIV

FileInfo fi=new FileInfo(Server.MapPath("TestDirectoryFile.doc"));
fi.CopyTo(Server.MapPath("NewTestDirectoryFile.doc"));

Create Method

Create Method creates a file.

Listing 44

FileInfo fi=new FileInfo(Server.MapPath("TestDirectoryFile.doc"));
fi.Create();

CreateText Method

CreateText Method writes a new text file.

Listing 45

FileInfo fi=new FileInfo(Server.MapPath("TestDirectoryFile.doc"));
fi.CreateText();

Delete Method

Delete Method deletes a file.

Listing 46

FileInfo fi=new FileInfo(Server.MapPath("TestDirectoryFile.doc"));
fi.Delete();

MoveTo Method

Move Method renames or moves an existing file.

Listing 47

FileInfo fi=new FileInfo(Server.MapPath("TestDirectoryFile.doc"));
fi.MoveTo(Server.MapPath("NewTestDirectoryFile.doc"));

Open Method

Open Method opens a file with various read or write and sharing privileges.  Once a file is opened and we have FileStream object, we can create a reader or writer object to work with the file’s contents.

Listing 48

FileStream fs;
StreamWriter sw;
FileInfo fis=new FileInfo(@"E:\TestDirectoryFile\test.txt");
fs=fis.Open(FileMode.Append,FileAccess.Write,FileShare.Write);
sw=new StreamWriter(fs);
sw.WriteLine("Write 1");
sw.WriteLine("Write 2");
sw.WriteLine("Write 3");
sw.Close();

View Entire Article

User Comments

No comments posted yet.






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 8:06:30 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search