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) Method
File.Open(System.String, System.IO.FileMode, System.IO.FileAccess) Method
File.Open(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare) 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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters. FileNotFoundException path was not found. ArgumentNullException path is null
.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.
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 IOException destFileName exists. -or-
An I/O error occurred.
ArgumentNullException sourceFileName or destFileName is null
.ArgumentException sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. -or-
sourceFileName or destFileName specifies a directory.
SecurityException The caller does not have the required permission. DirectoryNotFoundException Directory information in sourceFileName or destFileName was not found. FileNotFoundException sourceFileName was not found. PathTooLongException The length or the absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length.
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 IOException destFileName is read-only (write-protected), or destFileName exists and overwrite is false
.-or-
An I/O error occurred.
ArgumentNullException sourceFileName or destFileName is null
.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.
SecurityException The caller does not have the required permission. DirectoryNotFoundException Directory information in destFileName or sourceFileName was not found. FileNotFoundException sourceFileName was not found. PathTooLongException The length or the absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length.
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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters.
ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. IOException An I/O error occurred while creating the file. UnauthorizedAccessException path specified a file that is read-only (write-protected). PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length.
This method is equivalent to System.IO.File.Create(System.String) (path, System.IO.FileStream.DefaultBufferSize ).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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters.
ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. IOException An I/O error occurred while creating the file. UnauthorizedAccessException path specified a file that is read-only (write-protected). 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, 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 new 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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters. ArgumentNullException path is null
.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.
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-defined invalid characters. ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. IOException The specified file is in use. SecurityException The caller does not have the required permission. UnauthorizedAccessException path identifies a directory. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length.
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 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-defined 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. SecurityException 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-defined 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. SecurityException 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-defined 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. SecurityException 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 IOException The destination file is read-only or destFileName is a directory. ArgumentNullException sourceFileName or destFileName is null
.ArgumentException sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters.
SecurityException The caller does not have the required permission. DirectoryNotFoundException The directory information in sourceFileName or destFileName was not found. FileNotFoundException sourceFileName was not found or specifies a directory. PathTooLongException The length or absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length.
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);
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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters.
FileNotFoundException path was not found. ArgumentNullException path is null
.ArgumentOutOfRangeException mode specified an invalid value. DirectoryNotFoundException The directory information specified in path was not found. IOException An I/O error occurred while opening the file. 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.
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, 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 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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. -or-
access specified
Read
and mode specifiedCreate
,CreateNew
,Truncate
orAppend
.
FileNotFoundException path was not found. ArgumentNullException path is null
.ArgumentOutOfRangeException mode or access specified an invalid value. DirectoryNotFoundException The directory information specified in path was not found. IOException An I/O error occurred while opening the file. UnauthorizedAccessException path specified a read-only file and access is not Read
, or path specified a directory.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, 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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. -or-
access specified
Read
and mode specifiedCreate
,CreateNew
,Truncate
orAppend
.
FileNotFoundException path was not found. ArgumentNullException path is null
.ArgumentOutOfRangeException mode, access, or share specified an invalid value. DirectoryNotFoundException The directory information specified in path was not found. IOException An I/O error occurred while opening the file. UnauthorizedAccessException path specified a read-only file and access is not Read
, or path specified a directory.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, 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 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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. FileNotFoundException path was not found. ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. UnauthorizedAccessException path specified a read-only (write-protected) file or a directory. 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, 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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. FileNotFoundException path was not found. ArgumentNullException path is null
.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.
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 SecurityException The caller does not have the required permission. ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. FileNotFoundException path was not found. ArgumentNullException path is null
.DirectoryNotFoundException The directory information specified in path was not found. UnauthorizedAccessException path specified a read-only file or a directory. 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, 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-defined 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.
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-defined 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. SecurityException 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-defined 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. SecurityException 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