KEMBAR78
File Handling in .NET Framework | PDF | Computer File | Filename
0% found this document useful (0 votes)
11 views1 page

File Handling in .NET Framework

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

File Handling in .NET Framework

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

The File Class

The File Class

The File class provides the .NET Framework's support for all manner of file handling. With this class, you can
create, copy, delete, move, and open files and much more. File is a static or shared "operations" class and not
a class for objects. You cannot instantiate File and it has no constructor. To use File, simply reference it as
follows:

File.Copy("c:\doh.txt", "c:\ray.txt")

If you need to use file objects to do your file operations, use the FileInfo class, which contains instance
methods that work like the methods of the File class but live in objects. Static methods, being shared, incur
more security overhead, whereas instance methods do not always require security checks. Table 15−9 lists the
static (s) members of File.

Note See "Basic File Class Operations" at the end of this section for a discussion of the methods most
frequently used for standard file I/O operations.

You have the potential to raise exceptions if you provide malformed filenames and path information. The
following examples of paths will get processed by the File methods, but you should consider using the Path
class, discussed next, to help reduce path errors:

"c:\doh\ray.txt"
"c:\doh"
"doh\ray.txt" 'a relative path and file name
"\\doh\ray\" 'A UNC path for a server and share name.

Path

Programming against the file and directory classes is not rocket science, but the potential for problems in your
code is increased because you need to pass complex arguments (such as the various mode and access
constants discussed in the next section). One parameter that can be a minefield represents the path information
argument you need to pass to the various methods of the file and directory classes.

Table 15−9: The Static Methods of File

Member Purpose
AppendText Bridges to a StreamWriter that appends UTF−8 encoded text to an
existing file
Copy Copies a source file to a new target file
Create Creates a file on a given fully qualified path
CreateText Creates or opens a new file for writing UTF−8 encoded text
Delete Deletes the file on the given fully qualified path. An exception is not
thrown if the specified file does not exist
Exists Checks if a specific file exists
GetAttributes Retrieves the FileAttributes on the file on a given fully qualified path
GetCreationTime Retrieves the creation date and time of the specified file or directory
GetLastAccessTime Retrieves the date and time that the file or directory was last accessed
GetLastWriteTime Retrieves the date and time that the file or directory was last written to

521

You might also like