File Class
Assembly: mscorlib (in mscorlib.dll)
Use the File class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files. You can also use the File class to get and set file attributes or DateTime information related to the creation, access, and writing of a file.
Many of the File methods return other I/O types when you create or open files. You can use these other types to further manipulate a file. For more information, see specific File members such as OpenText, CreateText, or Create.
Because all File methods are static, it might be more efficient to use a File method rather than a corresponding FileInfo instance method if you want to perform only one action. All File methods require the path to the file that you are manipulating.
The static methods of the File class perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of FileInfo instead, because the security check will not always be necessary.
By default, full read/write access to new files is granted to all users.
The following table describes the enumerations that are used to customize the behavior of various File methods.
| Enumeration | Description |
|---|---|
| Specifies read and write access to a file. | |
| Specifies the level of access permitted for a file that is already in use. | |
| Specifies whether the contents of an existing file are preserved or overwritten, and whether requests to create an existing file cause an exception. |
Note |
|---|
| In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string. |
In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:
-
"c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.
-
"c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.
-
"MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.
-
"\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.
For an example of using this class, see the Example section. The following table lists examples of other typical or related I/O tasks.
| To do this... | See the example in this topic... |
|---|---|
| Create a text file. | |
| Write to a text file. | |
| Read from a text file. | |
| Append text to a file. | |
| Rename or move a file. | |
| Delete a file. | |
| Copy a file. | |
| Get the size of a file. | |
| Get the attributes of a file. | |
| Set the attributes of a file. | |
| Determine if a file exists. | |
| Read from a binary file. | |
| Write to a binary file. | |
| Retrieve a file extension. | |
| Retrieve the fully qualified path of a file. | |
| Retrieve the file name and extension from a path. | |
| Change the extension of a file. |
The following example demonstrates some of the main members of the File class.
Imports System Imports System.IO Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" If File.Exists(path) = False Then ' Create a file to write to. Dim sw As StreamWriter = File.CreateText(path) sw.WriteLine("Hello") sw.WriteLine("And") sw.WriteLine("Welcome") sw.Flush() sw.Close() End If Try ' Open the file to read from. Dim sr As StreamReader = File.OpenText(path) Do While sr.Peek() >= 0 Console.WriteLine(sr.ReadLine()) Loop sr.Close() Dim path2 As String = path + "temp" ' Ensure that the target does not exist. File.Delete(path2) ' Copy the file. File.Copy(path, path2) Console.WriteLine("{0} was copied to {1}.", path, path2) ' Delete the newly created file. File.Delete(path2) Console.WriteLine("{0} was successfully deleted.", path2) Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub End Class
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
String path = "c:\\temp\\MyTest.txt";
if (!(File.Exists(path))) {
// Create a file to write to.
StreamWriter sw = File.CreateText(path);
try {
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
finally {
sw.Dispose();
}
}
// Open the file to read from.
StreamReader sr = File.OpenText(path);
try {
String s = "";
while ((s = sr.ReadLine()) != null) {
Console.WriteLine(s);
}
}
finally {
sr.Dispose();
}
try {
String path2 = path + "temp";
// Ensure that the target does not exist.
File.Delete(path2);
// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
// Delete the newly created file.
File.Delete(path2);
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (System.Exception e) {
Console.WriteLine("The process failed: {0}", e.ToString());
}
} //main
} //Test
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note