public sealed class File
Object
File
mscorlib
BCL
Provides information and performs operations on files .
Implementations of this class are required to preserve the case of path strings. Implementations are required to be case sensitive if and only if the platform is case-sensitive.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.
System.IO Namespace
File Methods
File.AppendText Method
File.Copy(System.String, System.String) Method
File.Copy(System.String, System.String, bool) Method
File.Create(System.String) Method
File.Create(System.String, int) Method
File.CreateText Method
File.Delete Method
File.Exists Method
File.GetCreationTime Method
File.GetLastAccessTime Method
File.GetLastWriteTime Method
File.Move Method
File.Open(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare) Method
File.Open(System.String, System.IO.FileMode, System.IO.FileAccess) Method
File.Open(System.String, System.IO.FileMode) Method
File.OpenRead Method
File.OpenText Method
File.OpenWrite Method
File.SetCreationTime Method
File.SetLastAccessTime Method
File.SetLastWriteTime Method
public static StreamWriter AppendText(string path);
Appends UTF-8 encoded text to an existing file.
- path
- A String containing the name of the file to append to.
A StreamWriter that appends UTF-8 encoded text to the specified file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. System.IO.Exception A general I/O exception occurred, such as trying to access a CD-ROM drive whose tray is open. System.IO.NotSupportedException path is in an implementation-specific invalid format. PathTooLongException The length of path or the absolute path information for path exceeds the implementation-specific maximum length. UnauthorizedAccessException Access is denied. The caller does not have the required permission.
This method is equivalent to StreamWriter (path,true
). If the file specified by path does not exist, it is created. If the file does exist, writes to the StreamWriter append text to the file. Additional threads are permitted to read the file while it is open.The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static void Copy(string sourceFileName, string destFileName);
Copies the specified file to a new location.
- sourceFileName
- A String containing the name of the file to copy.
- destFileName
- A String containing the name of the destination file. Cannot specify a directory or an existing file.
Exception Type Condition ArgumentException sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. -or-
sourceFileName or destFileName specifies a directory.
ArgumentNullException sourceFileName or destFileName is null
.DirectoryNotFoundException Directory information in sourceFileName or destFileName was not found. FileNotFoundException sourceFileName was not found. IOException destFileName exists. -or-
An I/O error occurred.
PathTooLongException The length or the absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to System.IO.File.Copy(System.String,System.String) (sourceFileName, destFileName,false
).The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static void Copy(string sourceFileName, string destFileName, bool overwrite);
Copies the specified file to a new location.
- sourceFileName
- A String containing the name of the file to copy.
- destFileName
- A String containing the name of the destination file. Cannot specify the name of a directory.
- overwrite
- A Boolean value. Specify
true
if the destination file can be overwritten; otherwisefalse
.
Exception Type Condition ArgumentException sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters. -or-
sourceFileName or destFileName specifies a directory.
ArgumentNullException sourceFileName or destFileName is null
.DirectoryNotFoundException Directory information in destFileName or sourceFileName was not found. FileNotFoundException sourceFileName was not found. IOException destFileName is read-only (write-protected), or destFileName exists and overwrite is false
.-or-
An I/O error occurred.
PathTooLongException The length or the absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static FileStream Create(string path);
Creates or overwrites the specified file.
- path
- A String containing the name of the file.
A FileStream that provides read/write access to the specified file.
Exception Type Condition ArgumentNullException path is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
UnauthorizedAccessException path specified a file that is read-only (write-protected). DirectoryNotFoundException The directory information specified in path was not found. IOException An I/O error occurred while creating the file. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
If the specified file does not exist, it is created; if it does exist and it is not read-only, the contents are overwritten.The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static FileStream Create(string path, int bufferSize);
Creates or overwrites the specified file.
- path
- A String containing the name of the file.
- bufferSize
- A Int32 containing the number of bytes buffered for reads and writes to the file.
A FileStream that provides read/write access to the specified file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullException path is null
.UnauthorizedAccessException path specified a file that is read-only (write-protected). DirectoryNotFoundException The directory information specified in path was not found. IOException An I/O error occurred while creating the file. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to FileStream (path, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None , bufferSize).If the specified file does not exist, it is created; if it does exist and it is not read-only, the contents are overwritten.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static StreamWriter CreateText(string path);
Creates or opens a file for writing UTF-8 encoded text.
- path
- The file to be opened for writing.
A StreamWriter that writes to the specified file using UTF-8 encoding.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. System.IO.Exception A general I/O exception occurred, such as trying to access a CD-ROM drive whose tray is open. System.IO.NotSupportedException path is in an implementation-specific invalid format. PathTooLongException The length of path or the absolute path information for path exceeds the implementation-specific maximum length. UnauthorizedAccessException Access is denied. The caller does not have the required permission.
This method is equivalent to StreamWriter (path,false
). If the file specified by path does not exist, it is created. If the file does exist, its contents are overwritten. Additional threads are permitted to read the file while it is open.The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.
System.IO.File Class, System.IO Namespace
public static void Delete(string path);
Deletes the specified file.
- path
- A String containing the name of the file to be deleted.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.UnauthorizedAccessException path identifies a directory. -or-
path specifies a file that is read-only.
DirectoryNotFoundException The directory information specified in path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
[Note: Some implementations might throw IOException to cover such implementation-specific conditions as "file in use".]
System.IO.File Class, System.IO Namespace
public static bool Exists(string path);
Returns a Boolean indicating whether the specified file exists.
- path
- A String containing the name of the file to check.
true
if the caller has the required permissions and path contains the name of an existing file; otherwise,false
. If path isnull
or a zero-length string, returnsfalse
.
If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returnsfalse
regardless of the existence of path.The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static DateTime GetCreationTime(string path);
Returns the creation date and time of the specified file or directory.
- path
- A String containing the name of the file or directory for which to obtain creation date and time information.
A DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in local time.Platforms that do not support this feature return System.DateTime.MinValue.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.IOException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static DateTime GetLastAccessTime(string path);
Returns the date and time the specified file or directory was last accessed.
- path
- A String containing the name of the file or directory for which to obtain access date and time information.
A DateTime structure set to the date and time the specified file or directory was last accessed. This value is expressed in local time.Platforms that do not support this feature return System.DateTime.MinValue.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.IOException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static DateTime GetLastWriteTime(string path);
Returns the date and time the specified file or directory was last written to.
- path
- A String containing the name of the file for which to obtain write date and time information.
A DateTime structure set to the date and time the specified file or directory was last written to. This value is expressed in local time.Platforms that do not support this feature return System.DateTime.MinValue.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.IOException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static void Move(string sourceFileName, string destFileName);
Moves the specified file to a new location.
- sourceFileName
- A String containing the name of the file to move.
- destFileName
- A String containing the name of the new location for the file.
Exception Type Condition ArgumentException sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullException sourceFileName or destFileName is null
.DirectoryNotFoundException The directory information in sourceFileName or destFileName was not found. FileNotFoundException sourceFileName was not found or specifies a directory. IOException destFileName already exists or is a directory. PathTooLongException The length or absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method does not throw an exception if the source and destination are the same.The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share);
Opens a FileStream on the specified file.
- path
- A String containing the name of the file to open.
- mode
- A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
- access
- A FileAccess value that specifies the operations that can be performed on the file.
- share
- A FileShare value specifying the type of access other threads have to the file.
A FileStream that provides access to the specified file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. -or-
access specified
Read
and mode specifiedCreate
,CreateNew
,Truncate
orAppend
.
ArgumentNullException path is null
.ArgumentOutOfRangeException mode, access, or share specified an invalid value. UnauthorizedAccessException path specified a read-only file and access is not Read
, or path specified a directory.DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException path was not found. IOException An I/O error occurred while opening the file. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to FileStream (path, mode, access, share).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static FileStream Open(string path, FileMode mode, FileAccess access);
Opens a FileStream on the specified file.
- path
- A String containing the name of the file to open.
- mode
- A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
- access
- A FileAccess value that specifies the operations that can be performed on the file.
A FileStream that provides access to the specified file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. -or-
access specified
Read
and mode specifiedCreate
,CreateNew
,Truncate
orAppend
.
ArgumentNullException path is null
.ArgumentOutOfRangeException mode or access specified an invalid value. UnauthorizedAccessException path specified a read-only file and access is not Read
, or path specified a directory.DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException mode is System.IO.FileMode.Truncate or System.IO.FileMode.Open, but the specified file was not found. If a different mode is specified and the file was not found, a new one is created. IOException An I/O error occurred, such as specifying System.IO.FileMode.CreateNew when the file specified by path already exists. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length.
This method is equivalent to FileStream (path, mode, access, System.IO.FileShare.None ).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static FileStream Open(string path, FileMode mode);
Opens a FileStream on the specified file with read/write access.
- path
- A String containing the name of the file to open.
- mode
- A FileMode value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten.
A FileStream that provides read/write access to the specified file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullException path is null
.ArgumentOutOfRangeException mode specified an invalid value. UnauthorizedAccessException path specified a read-only file (this method attempts to open the file with read/write access). -or-
This operation is not supported on the current platform.
-or-
path specified a directory.
DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException path was not found. IOException An I/O error occurred while opening the file. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to FileStream (path, mode, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None ).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static FileStream OpenRead(string path);
Opens an existing file for reading.
- path
- A String containing the name of the file to be opened for reading.
A read-only FileStream containing the contents of the specified file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.UnauthorizedAccessException path specified a directory. DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to FileStream (path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read ).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static StreamReader OpenText(string path);
Opens an existing UTF-8 encoded text file for reading.
- path
- A String containing the name of the file to be opened for reading.
A StreamReader containing the contents of the specified file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to StreamReader (path).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static FileStream OpenWrite(string path);
Opens an existing file for writing.
- path
- A String containing the name of the file to be opened for writing.
A writable FileStream that writes to the file specified by path.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.UnauthorizedAccessException path specified a read-only file or a directory. DirectoryNotFoundException The directory information specified in path was not found. FileNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to FileStream (path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.None ).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.File Class, System.IO Namespace
public static void SetCreationTime(string path, DateTime creationTime);
Sets the creation date and time for the specified file.
- path
- A String containing the name of the file for which to set the creation date and time information.
- creationTime
- A DateTime containing the value to set for the creation date and time of path. This value is expressed in local time.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentOutOfRangeException creationTime specifies a value outside the range of date/times permitted for this operation. ArgumentNullException path is null
.FileNotFoundException path was not found. IOException An I/O error occurred while performing the operation. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
On platforms that do not support this feature, this method has no effect. If this feature is supported, the range of dates that is valid for this operation is implementation-specific.
System.IO.File Class, System.IO Namespace
public static void SetLastAccessTime(string path, DateTime lastAccessTime);
Sets the date and time the specified file was last accessed.
- path
- A String containing the name of the file for which to set the access date and time information.
- lastAccessTime
- A DateTime containing the value to set for the access date and time of path. This value is expressed in local time.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.ArgumentOutOfRangeException lastAccessTime specifies a value outside the range of date/times permitted for this operation. FileNotFoundException path was not found. IOException An I/O error occurred while performing the operation. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
On platforms that do not support this feature, this method has no effect. If this feature is supported, the range of dates that is valid for this operation is implementation-specific.
System.IO.File Class, System.IO Namespace
public static void SetLastWriteTime(string path, DateTime lastWriteTime);
Sets the date and time a file was last written to.
- path
- A String containing the name of the file for which to set the date and time information.
- lastWriteTime
- A DateTime containing the value to set for the last write date and time of path. This value is expressed in local time.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.ArgumentOutOfRangeException lastWriteTime specifies a value outside the range of date/times permitted for this operation. FileNotFoundException path was not found. IOException An I/O error occurred while performing the operation. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
On platforms that do not support this feature, this method has no effect. If this feature is supported, the range of dates that is valid for this operation is implementation-specific.
System.IO.File Class, System.IO Namespace