File |
Name | Description |
---|---|
AppendAllText | Appends the specified string to the file, creating the file if it does not already exist. |
AppendText | Creates a StreamWriter that appends UTF-8 encoded text to an existing file. |
Copy | Copies an existing file to a new file. Overwriting a file of the same name is allowed. |
Create | Creates or overwrites the specified file. |
CreateText | Creates or opens a file for writing UTF-8 encoded text. |
Delete | Deletes the specified file. |
Exists | Determines if a file exists. |
GetAttributes | Returns the file attributes (ReadOnly, Hidden, ...) |
GetCreationTime | Returns the time the file was created in local time. |
GetCreationTimeUtc | Returns the time the file was created in UTC time. |
GetLastAccessTime | Returns the time the file was accessed in local time. |
GetLastAccessTimeUtc | Returns The last time a file was accessed in UTC time. |
GetLastWriteTime | Returns the last time the file was written to in local time. |
GetLastWriteTimeUtc | Returns the last time the file was written to in UTC time. |
Move | Moves a file from one location to another. |
OpenFile | Opens a file with a FileStream |
OpenRead | Opens a file for reading as a FileStream. |
OpenText | Opens a file for reading through a StreamReader. |
OpenWrite | Opens a file for writing as a FileStream. |
ReadAllBytes | Reads an entire file into a byte array. |
ReadAllLines | Opens a file, reads all lines in a file into an array and closes the files. |
ReadAllText | Reads the entire contents of a file and returns it as a String. |
SetAttributes | Sets the file attributes (ReadOnly, Hidden, ...) |
SetCreationTime | Sets the time of creation for a file in local time. |
SetCreationTimeUtc | Sets the time of creation for a file in UTC time. |
SetLastAccessTime | Sets the last time the file was accessed in local time. |
SetLastAccessTimeUtc | Sets the last time the file was accessed in UTC time. |
SetLastWriteTime | Sets the last time the file was written to in local time. |
SetLastWriteTimeUtc | Sets the last time the file was written to in UTC time. |
WriteAllbytes | Opens a file, writes all bytes to the file, then closes the file. |
WriteAllLines | Opens a file, writes all strings to the file with appended new line values, then closes the file. |
WriteAllText | Opens a files, writes out all contents to the file, then closes the file. |
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 date and time 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.
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 |
---|---|
FileAccess | Specifies read and write access to a file. |
FileShare | Specifies the level of access permitted for a file that is already in use. |
FileMode | 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:
The following example demonstrates how to use the File class to check whether a file exists, and depending on the result, either create a new file and write to it, or open the existing file and read from it.
Public Sub Main() Const Path As String = "c:\temp\MyTest.txt" If Not File.Exists(Path) Then ' Create a file to write to. Dim sw As StreamWriter Set sw = File.CreateText(Path) sw.WriteLine "Hello" sw.WriteLine "And" sw.WriteLine "Welcome" sw.Flush End If ' Open the file to read from. Dim sr As StreamReader Set sr = File.OpenText(Path) Do While sr.Peek >= 0 Debug.Print sr.ReadLine Loop End Sub