Input/Output 3: File
Written by:
, 2009-04-16 21:24:38
The Basics
File is a basic class in the System.IO namespace. It can be used to control files on a computer and perform methods such as creating, copying and deleting files.
The File class constructor is private, so you cannot instantiate a file using the
new keyword. It's not necessary however since all the methods of File are static.
Getting into it
These are some important methods in the File class:
Create
Creates a file in a specified directory. You call this method passing a file path or a file path and an integer of a buffer size for the resulting FileStream object. For example, the following code creates a file called newFile.txt in the C:\data directory, returns a FileStream, and then closes the FileStream object straight away:
FileStream fs = File.Create("C:/data/newFile.txt");
fs.Close();
As another example, the following code creates a FileStream with a buffer size of 16KB.
FileStream fs = File.Create("
C:/123data/newFile.txt", 16384);
If the file c:\data\newfile.txt already exists, it will be overwritten.
CreateText
Creates a file and returns a StreamWriter object that you can use to write text into that file. Like Create, you pass a file path to the method, and any existing file will be overwritten. For example:
StreamWriter sw = File.CreateText("
C:/123data/new.txt");
AppendText
Returns a StreamWriter object to a specified existing file. The text you write will then be appended to the end of the file. If the file does not exist, AppendText will create a new file. You pass a file path to this method.
Exists
Returns true if a file in a specified path exists; otherwise, it returns false. Its only parameter is a path and filename.
Copy
Copies a file to another folder. There are two overloads of this method, as follows:
public static void Copy(
string sourceFileName,
string destFileName
);
public static void Copy(
string sourceFileName,
string destFileName,
bool overwrite
);
The first overload of the method will throw an IOException if the destination file already exists. So will the second overload, if the overwrite argument is false. For both overloads, a FileNotFoundException will be thrown if the source file does not exist. In addition, the method can throw an ArgumentException if either sourceFilename or destinationFilename is invalid.
Move
Moves a file to another directory. The Move method has the same syntax as the first overload of the Copy method. The difference from the Copy method is that Move deletes the source file.
Open
Opens a file and returns a FileStream object. If the file to be opened does not exist, a FileNotFoundException exception is thrown. This method has three overloads, all of which require that you pass a path and filename as the first argument and a member of the FileMode enumeration as the second. The second and third overloads require a member of the FileAccess enumeration as their third argument, while the third overload requires a member of the FileShare enumeration as its fourth argument.
The FileMode enumeration tells the operating system how the file should be opened. Its members are Append, Create, CreateNew, Open, OpenOrCreate, and Truncate.
The FileAccess enumeration indicates to the operating system whether the file is to be opened for reading or writing. Its members are Read, ReadWrite, and Write. The FileShare enumeration tells the operating system how to control access to the same opened file from other threads, processes, or users. Its members are Inheritable, None, Read, ReadWrite, and Write. If the value of the argument is FileShare.Write, for example, other programs can write to the opened file.
OpenRead
Opens a file as read-only and returns a FileStream object. Its only parameter is the path and name of the file. This is a single-purpose version of the Open method.
OpenText
Opens a file and returns a StreamReader object. Its only parameter is the path and name of the file.
OpenWrite
Opens a file and returns a Stream object. You can use this Stream object to read and write to the file. The method's only parameter is the path and name of the file.
GetAttributes
Obtains the file's attributes, as expressed in a bitwise combination of the members of the FileAttributes enumeration: Archive, Compressed, Device, Directory, Encrypted, Hidden, Normal, NotContentIndexed, Offline, ReadOnly, ReparsePoint, SparseFile, System, and Temporary.
Source:
http://www.ondotnet.com/pub/a/dotnet/2002/08/05/io.html?page=1