mscorlib(4.0.0.0) API with additions
System.IO.File Class Reference

Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of T:System.IO.FileStream objects.To browse the .NET Framework source code for this type, see the Reference Source. More...

Static Public Member Functions

static StreamReader OpenText (string path)
 Opens an existing UTF-8 encoded text file for reading. More...
 
static StreamWriter CreateText (string path)
 Creates or opens a file for writing UTF-8 encoded text. More...
 
static StreamWriter AppendText (string path)
 Creates a T:System.IO.StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist. More...
 
static void Copy (string sourceFileName, string destFileName)
 Copies an existing file to a new file. Overwriting a file of the same name is not allowed. More...
 
static void Copy (string sourceFileName, string destFileName, bool overwrite)
 Copies an existing file to a new file. Overwriting a file of the same name is allowed. More...
 
static FileStream Create (string path)
 Creates or overwrites a file in the specified path. More...
 
static FileStream Create (string path, int bufferSize)
 Creates or overwrites the specified file. More...
 
static FileStream Create (string path, int bufferSize, FileOptions options)
 Creates or overwrites the specified file, specifying a buffer size and a T:System.IO.FileOptions value that describes how to create or overwrite the file. More...
 
static FileStream Create (string path, int bufferSize, FileOptions options, FileSecurity fileSecurity)
 Creates or overwrites the specified file with the specified buffer size, file options, and file security. More...
 
static void Delete (string path)
 Deletes the specified file. More...
 
static void Decrypt (string path)
 Decrypts a file that was encrypted by the current account using the M:System.IO.File.Encrypt(System.String) method. More...
 
static void Encrypt (string path)
 Encrypts a file so that only the account used to encrypt the file can decrypt it. More...
 
static bool Exists (string path)
 Determines whether the specified file exists. More...
 
static FileStream Open (string path, FileMode mode)
 Opens a T:System.IO.FileStream on the specified path with read/write access. More...
 
static FileStream Open (string path, FileMode mode, FileAccess access)
 Opens a T:System.IO.FileStream on the specified path, with the specified mode and access. More...
 
static FileStream Open (string path, FileMode mode, FileAccess access, FileShare share)
 Opens a T:System.IO.FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. More...
 
static void SetCreationTime (string path, DateTime creationTime)
 Sets the date and time the file was created. More...
 
static unsafe void SetCreationTimeUtc (string path, DateTime creationTimeUtc)
 Sets the date and time, in coordinated universal time (UTC), that the file was created. More...
 
static DateTime GetCreationTime (string path)
 Returns the creation date and time of the specified file or directory. More...
 
static DateTime GetCreationTimeUtc (string path)
 Returns the creation date and time, in coordinated universal time (UTC), of the specified file or directory. More...
 
static void SetLastAccessTime (string path, DateTime lastAccessTime)
 Sets the date and time the specified file was last accessed. More...
 
static unsafe void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc)
 Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed. More...
 
static DateTime GetLastAccessTime (string path)
 Returns the date and time the specified file or directory was last accessed. More...
 
static DateTime GetLastAccessTimeUtc (string path)
 Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed. More...
 
static void SetLastWriteTime (string path, DateTime lastWriteTime)
 Sets the date and time that the specified file was last written to. More...
 
static unsafe void SetLastWriteTimeUtc (string path, DateTime lastWriteTimeUtc)
 Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to. More...
 
static DateTime GetLastWriteTime (string path)
 Returns the date and time the specified file or directory was last written to. More...
 
static DateTime GetLastWriteTimeUtc (string path)
 Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to. More...
 
static FileAttributes GetAttributes (string path)
 Gets the T:System.IO.FileAttributes of the file on the path. More...
 
static void SetAttributes (string path, FileAttributes fileAttributes)
 Sets the specified T:System.IO.FileAttributes of the file on the specified path. More...
 
static FileSecurity GetAccessControl (string path)
 Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the access control list (ACL) entries for a specified file. More...
 
static FileSecurity GetAccessControl (string path, AccessControlSections includeSections)
 Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the specified type of access control list (ACL) entries for a particular file. More...
 
static void SetAccessControl (string path, FileSecurity fileSecurity)
 Applies access control list (ACL) entries described by a T:System.Security.AccessControl.FileSecurity object to the specified file. More...
 
static FileStream OpenRead (string path)
 Opens an existing file for reading. More...
 
static FileStream OpenWrite (string path)
 Opens an existing file or creates a new file for writing. More...
 
static string ReadAllText (string path)
 Opens a text file, reads all lines of the file, and then closes the file. More...
 
static string ReadAllText (string path, Encoding encoding)
 Opens a file, reads all lines of the file with the specified encoding, and then closes the file. More...
 
static void WriteAllText (string path, string contents)
 Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. More...
 
static void WriteAllText (string path, string contents, Encoding encoding)
 Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten. More...
 
static byte [] ReadAllBytes (string path)
 Opens a binary file, reads the contents of the file into a byte array, and then closes the file. More...
 
static void WriteAllBytes (string path, byte[] bytes)
 Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten. More...
 
static string [] ReadAllLines (string path)
 Opens a text file, reads all lines of the file, and then closes the file. More...
 
static string [] ReadAllLines (string path, Encoding encoding)
 Opens a file, reads all lines of the file with the specified encoding, and then closes the file. More...
 
static IEnumerable< string > ReadLines (string path)
 Reads the lines of a file. More...
 
static IEnumerable< string > ReadLines (string path, Encoding encoding)
 Read the lines of a file that has a specified encoding. More...
 
static void WriteAllLines (string path, string[] contents)
 Creates a new file, write the specified string array to the file, and then closes the file. More...
 
static void WriteAllLines (string path, string[] contents, Encoding encoding)
 Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file. More...
 
static void WriteAllLines (string path, IEnumerable< string > contents)
 Creates a new file, writes a collection of strings to the file, and then closes the file. More...
 
static void WriteAllLines (string path, IEnumerable< string > contents, Encoding encoding)
 Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file. More...
 
static void AppendAllText (string path, string contents)
 Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file. More...
 
static void AppendAllText (string path, string contents, Encoding encoding)
 Appends the specified string to the file, creating the file if it does not already exist. More...
 
static void AppendAllLines (string path, IEnumerable< string > contents)
 Appends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file. More...
 
static void AppendAllLines (string path, IEnumerable< string > contents, Encoding encoding)
 Appends lines to a file by using a specified encoding, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file. More...
 
static void Move (string sourceFileName, string destFileName)
 Moves a specified file to a new location, providing the option to specify a new file name. More...
 
static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName)
 Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file. More...
 
static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
 Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file and optionally ignores merge errors. More...
 

Detailed Description

Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of T:System.IO.FileStream objects.To browse the .NET Framework source code for this type, see the Reference Source.

Definition at line 14 of file File.cs.

Member Function Documentation

◆ AppendAllLines() [1/2]

static void System.IO.File.AppendAllLines ( string  path,
IEnumerable< string >  contents 
)
static

Appends lines to a file, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.

Parameters
pathThe file to append the lines to. The file is created if it doesn't already exist.
contentsThe lines to append to the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one more invalid characters defined by the M:System.IO.Path.GetInvalidPathChars method.
T:System.ArgumentNullExceptionEither path or contents is null.
T:System.IO.DirectoryNotFoundExceptionpath is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionThe file specified by path was not found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.IO.PathTooLongExceptionpath exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have permission to write to the file.
T:System.UnauthorizedAccessExceptionpath specifies a file that is read-only.-or-This operation is not supported on the current platform.-or- path is a directory.

Definition at line 1688 of file File.cs.

◆ AppendAllLines() [2/2]

static void System.IO.File.AppendAllLines ( string  path,
IEnumerable< string >  contents,
Encoding  encoding 
)
static

Appends lines to a file by using a specified encoding, and then closes the file. If the specified file does not exist, this method creates a file, writes the specified lines to the file, and then closes the file.

Parameters
pathThe file to append the lines to. The file is created if it doesn't already exist.
contentsThe lines to append to the file.
encodingThe character encoding to use.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one more invalid characters defined by the M:System.IO.Path.GetInvalidPathChars method.
T:System.ArgumentNullExceptionEither path , contents , or encoding is null.
T:System.IO.DirectoryNotFoundExceptionpath is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionThe file specified by path was not found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.IO.PathTooLongExceptionpath exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.
T:System.UnauthorizedAccessExceptionpath specifies a file that is read-only.-or-This operation is not supported on the current platform.-or- path is a directory.-or-The caller does not have the required permission.

Definition at line 1724 of file File.cs.

◆ AppendAllText() [1/2]

static void System.IO.File.AppendAllText ( string  path,
string  contents 
)
static

Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.

Parameters
pathThe file to append the specified string to.
contentsThe string to append to the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1615 of file File.cs.

◆ AppendAllText() [2/2]

static void System.IO.File.AppendAllText ( string  path,
string  contents,
Encoding  encoding 
)
static

Appends the specified string to the file, creating the file if it does not already exist.

Parameters
pathThe file to append the specified string to.
contentsThe string to append to the file.
encodingThe character encoding to use.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1645 of file File.cs.

◆ AppendText()

static StreamWriter System.IO.File.AppendText ( string  path)
static

Creates a T:System.IO.StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.

Parameters
pathThe path to the file to append to.
Returns
A stream writer that appends UTF-8 encoded text to the specified file or to a new file.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 77 of file File.cs.

◆ Copy() [1/2]

static void System.IO.File.Copy ( string  sourceFileName,
string  destFileName 
)
static

Copies an existing file to a new file. Overwriting a file of the same name is not allowed.

Parameters
sourceFileNameThe file to copy.
destFileNameThe name of the destination file. This cannot be a directory or an existing file.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionsourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.-or- sourceFileName or destFileName specifies a directory.
T:System.ArgumentNullExceptionsourceFileName or destFileName is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionsourceFileName was not found.
T:System.IO.IOExceptiondestFileName exists.-or- An I/O error has occurred.
T:System.NotSupportedExceptionsourceFileName or destFileName is in an invalid format.

Definition at line 103 of file File.cs.

◆ Copy() [2/2]

static void System.IO.File.Copy ( string  sourceFileName,
string  destFileName,
bool  overwrite 
)
static

Copies an existing file to a new file. Overwriting a file of the same name is allowed.

Parameters
sourceFileNameThe file to copy.
destFileNameThe name of the destination file. This cannot be a directory.
overwritetrue if the destination file can be overwritten; otherwise, false.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission. -or- destFileName is read-only.
T:System.ArgumentExceptionsourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.-or- sourceFileName or destFileName specifies a directory.
T:System.ArgumentNullExceptionsourceFileName or destFileName is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionsourceFileName was not found.
T:System.IO.IOExceptiondestFileName exists and overwrite is false.-or- An I/O error has occurred.
T:System.NotSupportedExceptionsourceFileName or destFileName is in an invalid format.

Definition at line 144 of file File.cs.

◆ Create() [1/4]

static FileStream System.IO.File.Create ( string  path)
static

Creates or overwrites a file in the specified path.

Parameters
pathThe path and name of the file to create.
Returns
A T:System.IO.FileStream that provides read/write access to the file specified in path .
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while creating the file.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 231 of file File.cs.

◆ Create() [2/4]

static FileStream System.IO.File.Create ( string  path,
int  bufferSize 
)
static

Creates or overwrites the specified file.

Parameters
pathThe name of the file.
bufferSizeThe number of bytes buffered for reads and writes to the file.
Returns
A T:System.IO.FileStream with the specified buffer size that provides read/write access to the file specified in path .
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while creating the file.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 251 of file File.cs.

◆ Create() [3/4]

static FileStream System.IO.File.Create ( string  path,
int  bufferSize,
FileOptions  options 
)
static

Creates or overwrites the specified file, specifying a buffer size and a T:System.IO.FileOptions value that describes how to create or overwrite the file.

Parameters
pathThe name of the file.
bufferSizeThe number of bytes buffered for reads and writes to the file.
optionsOne of the T:System.IO.FileOptions values that describes how to create or overwrite the file.
Returns
A new file with the specified buffer size.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only. -or- F:System.IO.FileOptions.Encrypted is specified for options and file encryption is not supported on the current platform.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive.
T:System.IO.IOExceptionAn I/O error occurred while creating the file.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only.

Definition at line 277 of file File.cs.

◆ Create() [4/4]

static FileStream System.IO.File.Create ( string  path,
int  bufferSize,
FileOptions  options,
FileSecurity  fileSecurity 
)
static

Creates or overwrites the specified file with the specified buffer size, file options, and file security.

Parameters
pathThe name of the file.
bufferSizeThe number of bytes buffered for reads and writes to the file.
optionsOne of the T:System.IO.FileOptions values that describes how to create or overwrite the file.
fileSecurityOne of the T:System.Security.AccessControl.FileSecurity values that determines the access control and audit security for the file.
Returns
A new file with the specified buffer size, file options, and file security.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only.-or- F:System.IO.FileOptions.Encrypted is specified for options and file encryption is not supported on the current platform.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while creating the file.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a file that is read-only.

Definition at line 304 of file File.cs.

◆ CreateText()

static StreamWriter System.IO.File.CreateText ( string  path)
static

Creates or opens a file for writing UTF-8 encoded text.

Parameters
pathThe file to be opened for writing.
Returns
A T:System.IO.StreamWriter that writes to the specified file using UTF-8 encoding.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 56 of file File.cs.

◆ Decrypt()

static void System.IO.File.Decrypt ( string  path)
static

Decrypts a file that was encrypted by the current account using the M:System.IO.File.Encrypt(System.String) method.

Parameters
pathA path that describes a file to decrypt.
Exceptions
T:System.ArgumentExceptionThe path parameter is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionThe path parameter is null.
T:System.IO.DriveNotFoundExceptionAn invalid drive was specified.
T:System.IO.FileNotFoundExceptionThe file described by the path parameter could not be found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file. For example, the encrypted file is already open. -or-This operation is not supported on the current platform.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.PlatformNotSupportedExceptionThe current operating system is not Windows NT or later.
T:System.NotSupportedExceptionThe file system is not NTFS.
T:System.UnauthorizedAccessExceptionThe path parameter specified a file that is read-only.-or- This operation is not supported on the current platform.-or- The path parameter specified a directory.-or- The caller does not have the required permission.

Definition at line 370 of file File.cs.

◆ Delete()

static void System.IO.File.Delete ( string  path)
static

Deletes the specified file.

Parameters
pathThe name of the file to be deleted. Wildcard characters are not supported.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionThe specified file is in use. -or-There is an open handle on the file, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files. For more information, see How to: Enumerate Directories and Files.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- The file is an executable file that is in use.-or- path is a directory.-or- path specified a read-only file.

Definition at line 324 of file File.cs.

◆ Encrypt()

static void System.IO.File.Encrypt ( string  path)
static

Encrypts a file so that only the account used to encrypt the file can decrypt it.

Parameters
pathA path that describes a file to encrypt.
Exceptions
T:System.ArgumentExceptionThe path parameter is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionThe path parameter is null.
T:System.IO.DriveNotFoundExceptionAn invalid drive was specified.
T:System.IO.FileNotFoundExceptionThe file described by the path parameter could not be found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.-or-This operation is not supported on the current platform.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.PlatformNotSupportedExceptionThe current operating system is not Windows NT or later.
T:System.NotSupportedExceptionThe file system is not NTFS.
T:System.UnauthorizedAccessExceptionThe path parameter specified a file that is read-only.-or- This operation is not supported on the current platform.-or- The path parameter specified a directory.-or- The caller does not have the required permission.

Definition at line 406 of file File.cs.

◆ Exists()

static bool System.IO.File.Exists ( string  path)
static

Determines whether the specified file exists.

Parameters
pathThe file to check.
Returns
true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. This method also returns false if path is null, an invalid path, or a zero-length string. If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path .

Definition at line 435 of file File.cs.

◆ GetAccessControl() [1/2]

static FileSecurity System.IO.File.GetAccessControl ( string  path)
static

Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the access control list (ACL) entries for a specified file.

Parameters
pathThe path to a file containing a T:System.Security.AccessControl.FileSecurity object that describes the file's access control list (ACL) information.
Returns
A T:System.Security.AccessControl.FileSecurity object that encapsulates the access control rules for the file described by the path parameter.
Exceptions
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.Runtime.InteropServices.SEHExceptionThe path parameter is null.
T:System.SystemExceptionThe file could not be found.
T:System.UnauthorizedAccessExceptionThe path parameter specified a file that is read-only.-or- This operation is not supported on the current platform.-or- The path parameter specified a directory.-or- The caller does not have the required permission.

Definition at line 933 of file File.cs.

◆ GetAccessControl() [2/2]

static FileSecurity System.IO.File.GetAccessControl ( string  path,
AccessControlSections  includeSections 
)
static

Gets a T:System.Security.AccessControl.FileSecurity object that encapsulates the specified type of access control list (ACL) entries for a particular file.

Parameters
pathThe path to a file containing a T:System.Security.AccessControl.FileSecurity object that describes the file's access control list (ACL) information.
includeSectionsOne of the T:System.Security.AccessControl.AccessControlSections values that specifies the type of access control list (ACL) information to receive.
Returns
A T:System.Security.AccessControl.FileSecurity object that encapsulates the access control rules for the file described by the path parameter.
Exceptions
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.Runtime.InteropServices.SEHExceptionThe path parameter is null.
T:System.SystemExceptionThe file could not be found.
T:System.UnauthorizedAccessExceptionThe path parameter specified a file that is read-only.-or- This operation is not supported on the current platform.-or- The path parameter specified a directory.-or- The caller does not have the required permission.

Definition at line 946 of file File.cs.

◆ GetAttributes()

static FileAttributes System.IO.File.GetAttributes ( string  path)
static

Gets the T:System.IO.FileAttributes of the file on the path.

Parameters
pathThe path to the file.
Returns
The T:System.IO.FileAttributes of the file on the path.
Exceptions
T:System.ArgumentExceptionpath is empty, contains only white spaces, or contains invalid characters.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.IO.FileNotFoundExceptionpath represents a file and is invalid, such as being on an unmapped drive, or the file cannot be found.
T:System.IO.DirectoryNotFoundExceptionpath represents a directory and is invalid, such as being on an unmapped drive, or the directory cannot be found.
T:System.IO.IOExceptionThis file is being used by another process.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.

Definition at line 884 of file File.cs.

◆ GetCreationTime()

static DateTime System.IO.File.GetCreationTime ( string  path)
static

Returns the creation date and time of the specified file or directory.

Parameters
pathThe file or directory for which to obtain creation date and time information.
Returns
A T:System.DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in local time.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 638 of file File.cs.

◆ GetCreationTimeUtc()

static DateTime System.IO.File.GetCreationTimeUtc ( string  path)
static

Returns the creation date and time, in coordinated universal time (UTC), of the specified file or directory.

Parameters
pathThe file or directory for which to obtain creation date and time information.
Returns
A T:System.DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in UTC time.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 655 of file File.cs.

◆ GetLastAccessTime()

static DateTime System.IO.File.GetLastAccessTime ( string  path)
static

Returns the date and time the specified file or directory was last accessed.

Parameters
pathThe file or directory for which to obtain access date and time information.
Returns
A T:System.DateTime structure set to the date and time that the specified file or directory was last accessed. This value is expressed in local time.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 735 of file File.cs.

◆ GetLastAccessTimeUtc()

static DateTime System.IO.File.GetLastAccessTimeUtc ( string  path)
static

Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.

Parameters
pathThe file or directory for which to obtain access date and time information.
Returns
A T:System.DateTime structure set to the date and time that the specified file or directory was last accessed. This value is expressed in UTC time.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 752 of file File.cs.

◆ GetLastWriteTime()

static DateTime System.IO.File.GetLastWriteTime ( string  path)
static

Returns the date and time the specified file or directory was last written to.

Parameters
pathThe file or directory for which to obtain write date and time information.
Returns
A T:System.DateTime structure set to the date and time that the specified file or directory was last written to. This value is expressed in local time.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 832 of file File.cs.

◆ GetLastWriteTimeUtc()

static DateTime System.IO.File.GetLastWriteTimeUtc ( string  path)
static

Returns the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.

Parameters
pathThe file or directory for which to obtain write date and time information.
Returns
A T:System.DateTime structure set to the date and time that the specified file or directory was last written to. This value is expressed in UTC time.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 849 of file File.cs.

◆ Move()

static void System.IO.File.Move ( string  sourceFileName,
string  destFileName 
)
static

Moves a specified file to a new location, providing the option to specify a new file name.

Parameters
sourceFileNameThe name of the file to move. Can include a relative or absolute path.
destFileNameThe new path and name for the file.
Exceptions
T:System.IO.IOExceptionThe destination file already exists.-or- sourceFileName was not found.
T:System.ArgumentNullExceptionsourceFileName or destFileName is null.
T:System.ArgumentExceptionsourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters as defined in F:System.IO.Path.InvalidPathChars.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe path specified in sourceFileName or destFileName is invalid, (for example, it is on an unmapped drive).
T:System.NotSupportedExceptionsourceFileName or destFileName is in an invalid format.

Definition at line 1760 of file File.cs.

◆ Open() [1/3]

static FileStream System.IO.File.Open ( string  path,
FileMode  mode 
)
static

Opens a T:System.IO.FileStream on the specified path with read/write access.

Parameters
pathThe file to open.
modeA T:System.IO.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.
Returns
A T:System.IO.FileStream opened in the specified mode and path, with read/write access and not shared.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid, (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission. -or- mode is F:System.IO.FileMode.Create and the specified file is a hidden file.
T:System.ArgumentOutOfRangeExceptionmode specified an invalid value.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 516 of file File.cs.

◆ Open() [2/3]

static FileStream System.IO.File.Open ( string  path,
FileMode  mode,
FileAccess  access 
)
static

Opens a T:System.IO.FileStream on the specified path, with the specified mode and access.

Parameters
pathThe file to open.
modeA T:System.IO.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.
accessA T:System.IO.FileAccess value that specifies the operations that can be performed on the file.
Returns
An unshared T:System.IO.FileStream that provides access to the specified file, with the specified mode and access.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.-or- access specified Read and mode specified Create, CreateNew, Truncate, or Append.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid, (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only and access is not Read.-or- path specified a directory.-or- The caller does not have the required permission. -or- mode is F:System.IO.FileMode.Create and the specified file is a hidden file.
T:System.ArgumentOutOfRangeExceptionmode or access specified an invalid value.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 543 of file File.cs.

◆ Open() [3/3]

static FileStream System.IO.File.Open ( string  path,
FileMode  mode,
FileAccess  access,
FileShare  share 
)
static

Opens a T:System.IO.FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.

Parameters
pathThe file to open.
modeA T:System.IO.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.
accessA T:System.IO.FileAccess value that specifies the operations that can be performed on the file.
shareA T:System.IO.FileShare value specifying the type of access other threads have to the file.
Returns
A T:System.IO.FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.-or- access specified Read and mode specified Create, CreateNew, Truncate, or Append.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid, (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only and access is not Read.-or- path specified a directory.-or- The caller does not have the required permission. -or- mode is F:System.IO.FileMode.Create and the specified file is a hidden file.
T:System.ArgumentOutOfRangeExceptionmode , access , or share specified an invalid value.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 571 of file File.cs.

◆ OpenRead()

static FileStream System.IO.File.OpenRead ( string  path)
static

Opens an existing file for reading.

Parameters
pathThe file to be opened for reading.
Returns
A read-only T:System.IO.FileStream on the specified path.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid, (for example, it is on an unmapped drive).
T:System.UnauthorizedAccessExceptionpath specified a directory.-or- The caller does not have the required permission.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.

Definition at line 985 of file File.cs.

◆ OpenText()

static StreamReader System.IO.File.OpenText ( string  path)
static

Opens an existing UTF-8 encoded text file for reading.

Parameters
pathThe file to be opened for reading.
Returns
A T:System.IO.StreamReader on the specified path.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid, (for example, it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 35 of file File.cs.

◆ OpenWrite()

static FileStream System.IO.File.OpenWrite ( string  path)
static

Opens an existing file or creates a new file for writing.

Parameters
pathThe file to be opened for writing.
Returns
An unshared T:System.IO.FileStream object on the specified path with F:System.IO.FileAccess.Write access.
Exceptions
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.-or- path specified a read-only file or directory.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid, (for example, it is on an unmapped drive).
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 1003 of file File.cs.

◆ ReadAllBytes()

static byte [] System.IO.File.ReadAllBytes ( string  path)
static

Opens a binary file, reads the contents of the file into a byte array, and then closes the file.

Parameters
pathThe file to open for reading.
Returns
A byte array containing the contents of the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionThis operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1203 of file File.cs.

◆ ReadAllLines() [1/2]

static string [] System.IO.File.ReadAllLines ( string  path)
static

Opens a text file, reads all lines of the file, and then closes the file.

Parameters
pathThe file to open for reading.
Returns
A string array containing all lines of the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1319 of file File.cs.

◆ ReadAllLines() [2/2]

static string [] System.IO.File.ReadAllLines ( string  path,
Encoding  encoding 
)
static

Opens a file, reads all lines of the file with the specified encoding, and then closes the file.

Parameters
pathThe file to open for reading.
encodingThe encoding applied to the contents of the file.
Returns
A string array containing all lines of the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1350 of file File.cs.

◆ ReadAllText() [1/2]

static string System.IO.File.ReadAllText ( string  path)
static

Opens a text file, reads all lines of the file, and then closes the file.

Parameters
pathThe file to open for reading.
Returns
A string containing all lines of the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1026 of file File.cs.

◆ ReadAllText() [2/2]

static string System.IO.File.ReadAllText ( string  path,
Encoding  encoding 
)
static

Opens a file, reads all lines of the file with the specified encoding, and then closes the file.

Parameters
pathThe file to open for reading.
encodingThe encoding applied to the contents of the file.
Returns
A string containing all lines of the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.IO.FileNotFoundExceptionThe file specified in path was not found.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1058 of file File.cs.

◆ ReadLines() [1/2]

static IEnumerable<string> System.IO.File.ReadLines ( string  path)
static

Reads the lines of a file.

Parameters
pathThe file to read.
Returns
All the lines of the file, or the lines that are the result of a query.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters defined by the M:System.IO.Path.GetInvalidPathChars method.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.DirectoryNotFoundExceptionpath is invalid (for example, it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionThe file specified by path was not found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.IO.PathTooLongExceptionpath exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
T:System.Security.SecurityExceptionThe caller does not have the required permission.
T:System.UnauthorizedAccessExceptionpath specifies a file that is read-only.-or-This operation is not supported on the current platform.-or- path is a directory.-or-The caller does not have the required permission.

Definition at line 1398 of file File.cs.

◆ ReadLines() [2/2]

static IEnumerable<string> System.IO.File.ReadLines ( string  path,
Encoding  encoding 
)
static

Read the lines of a file that has a specified encoding.

Parameters
pathThe file to read.
encodingThe encoding that is applied to the contents of the file.
Returns
All the lines of the file, or the lines that are the result of a query.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by the M:System.IO.Path.GetInvalidPathChars method.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.DirectoryNotFoundExceptionpath is invalid (for example, it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionThe file specified by path was not found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.IO.PathTooLongExceptionpath exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
T:System.Security.SecurityExceptionThe caller does not have the required permission.
T:System.UnauthorizedAccessExceptionpath specifies a file that is read-only.-or-This operation is not supported on the current platform.-or- path is a directory.-or-The caller does not have the required permission.

Definition at line 1429 of file File.cs.

◆ Replace() [1/2]

static void System.IO.File.Replace ( string  sourceFileName,
string  destinationFileName,
string  destinationBackupFileName 
)
static

Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file.

Parameters
sourceFileNameThe name of a file that replaces the file specified by destinationFileName .
destinationFileNameThe name of the file being replaced.
destinationBackupFileNameThe name of the backup file.
Exceptions
T:System.ArgumentExceptionThe path described by the destinationFileName parameter was not of a legal form.-or-The path described by the destinationBackupFileName parameter was not of a legal form.
T:System.ArgumentNullExceptionThe destinationFileName parameter is null.
T:System.IO.DriveNotFoundExceptionAn invalid drive was specified.
T:System.IO.FileNotFoundExceptionThe file described by the current T:System.IO.FileInfo object could not be found.-or-The file described by the destinationBackupFileName parameter could not be found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.- or -The sourceFileName and destinationFileName parameters specify the same file.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.PlatformNotSupportedExceptionThe operating system is Windows 98 Second Edition or earlier and the files system is not NTFS.
T:System.UnauthorizedAccessExceptionThe sourceFileName or destinationFileName parameter specifies a file that is read-only.-or- This operation is not supported on the current platform.-or- Source or destination parameters specify a directory instead of a file.-or- The caller does not have the required permission.

Definition at line 1816 of file File.cs.

◆ Replace() [2/2]

static void System.IO.File.Replace ( string  sourceFileName,
string  destinationFileName,
string  destinationBackupFileName,
bool  ignoreMetadataErrors 
)
static

Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file and optionally ignores merge errors.

Parameters
sourceFileNameThe name of a file that replaces the file specified by destinationFileName .
destinationFileNameThe name of the file being replaced.
destinationBackupFileNameThe name of the backup file.
ignoreMetadataErrorstrue to ignore merge errors (such as attributes and access control lists (ACLs)) from the replaced file to the replacement file; otherwise, false.
Exceptions
T:System.ArgumentExceptionThe path described by the destinationFileName parameter was not of a legal form.-or-The path described by the destinationBackupFileName parameter was not of a legal form.
T:System.ArgumentNullExceptionThe destinationFileName parameter is null.
T:System.IO.DriveNotFoundExceptionAn invalid drive was specified.
T:System.IO.FileNotFoundExceptionThe file described by the current T:System.IO.FileInfo object could not be found.-or-The file described by the destinationBackupFileName parameter could not be found.
T:System.IO.IOExceptionAn I/O error occurred while opening the file.- or -The sourceFileName and destinationFileName parameters specify the same file.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.PlatformNotSupportedExceptionThe operating system is Windows 98 Second Edition or earlier and the files system is not NTFS.
T:System.UnauthorizedAccessExceptionThe sourceFileName or destinationFileName parameter specifies a file that is read-only.-or- This operation is not supported on the current platform.-or- Source or destination parameters specify a directory instead of a file.-or- The caller does not have the required permission.

Definition at line 1843 of file File.cs.

◆ SetAccessControl()

static void System.IO.File.SetAccessControl ( string  path,
FileSecurity  fileSecurity 
)
static

Applies access control list (ACL) entries described by a T:System.Security.AccessControl.FileSecurity object to the specified file.

Parameters
pathA file to add or remove access control list (ACL) entries from.
fileSecurityA T:System.Security.AccessControl.FileSecurity object that describes an ACL entry to apply to the file described by the path parameter.
Exceptions
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.Runtime.InteropServices.SEHExceptionThe path parameter is null.
T:System.SystemExceptionThe file could not be found.
T:System.UnauthorizedAccessExceptionThe path parameter specified a file that is read-only.-or- This operation is not supported on the current platform.-or- The path parameter specified a directory.-or- The caller does not have the required permission.
T:System.ArgumentNullExceptionThe fileSecurity parameter is null.

Definition at line 960 of file File.cs.

◆ SetAttributes()

static void System.IO.File.SetAttributes ( string  path,
FileAttributes  fileAttributes 
)
static

Sets the specified T:System.IO.FileAttributes of the file on the specified path.

Parameters
pathThe path to the file.
fileAttributesA bitwise combination of the enumeration values.
Exceptions
T:System.ArgumentExceptionpath is empty, contains only white spaces, contains invalid characters, or the file attribute is invalid.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid, (for example, it is on an unmapped drive).
T:System.IO.FileNotFoundExceptionThe file cannot be found.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.

Definition at line 911 of file File.cs.

◆ SetCreationTime()

static void System.IO.File.SetCreationTime ( string  path,
DateTime  creationTime 
)
static

Sets the date and time the file was created.

Parameters
pathThe file for which to set the creation date and time information.
creationTimeA T:System.DateTime containing the value to set for the creation date and time of path . This value is expressed in local time.
Exceptions
T:System.IO.FileNotFoundExceptionThe specified path was not found.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.IOExceptionAn I/O error occurred while performing the operation.
T:System.ArgumentOutOfRangeExceptioncreationTime specifies a value outside the range of dates, times, or both permitted for this operation.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 591 of file File.cs.

◆ SetCreationTimeUtc()

static unsafe void System.IO.File.SetCreationTimeUtc ( string  path,
DateTime  creationTimeUtc 
)
static

Sets the date and time, in coordinated universal time (UTC), that the file was created.

Parameters
pathThe file for which to set the creation date and time information.
creationTimeUtcA T:System.DateTime containing the value to set for the creation date and time of path . This value is expressed in UTC time.
Exceptions
T:System.IO.FileNotFoundExceptionThe specified path was not found.
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.IOExceptionAn I/O error occurred while performing the operation.
T:System.ArgumentOutOfRangeExceptioncreationTime specifies a value outside the range of dates, times, or both permitted for this operation.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.

Definition at line 612 of file File.cs.

◆ SetLastAccessTime()

static void System.IO.File.SetLastAccessTime ( string  path,
DateTime  lastAccessTime 
)
static

Sets the date and time the specified file was last accessed.

Parameters
pathThe file for which to set the access date and time information.
lastAccessTimeA T:System.DateTime containing the value to set for the last access date and time of path . This value is expressed in local time.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.FileNotFoundExceptionThe specified path was not found.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.ArgumentOutOfRangeExceptionlastAccessTime specifies a value outside the range of dates or times permitted for this operation.

Definition at line 689 of file File.cs.

◆ SetLastAccessTimeUtc()

static unsafe void System.IO.File.SetLastAccessTimeUtc ( string  path,
DateTime  lastAccessTimeUtc 
)
static

Sets the date and time, in coordinated universal time (UTC), that the specified file was last accessed.

Parameters
pathThe file for which to set the access date and time information.
lastAccessTimeUtcA T:System.DateTime containing the value to set for the last access date and time of path . This value is expressed in UTC time.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.FileNotFoundExceptionThe specified path was not found.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.ArgumentOutOfRangeExceptionlastAccessTimeUtc specifies a value outside the range of dates or times permitted for this operation.

Definition at line 709 of file File.cs.

◆ SetLastWriteTime()

static void System.IO.File.SetLastWriteTime ( string  path,
DateTime  lastWriteTime 
)
static

Sets the date and time that the specified file was last written to.

Parameters
pathThe file for which to set the date and time information.
lastWriteTimeA T:System.DateTime containing the value to set for the last write date and time of path . This value is expressed in local time.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.FileNotFoundExceptionThe specified path was not found.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.ArgumentOutOfRangeExceptionlastWriteTime specifies a value outside the range of dates or times permitted for this operation.

Definition at line 786 of file File.cs.

◆ SetLastWriteTimeUtc()

static unsafe void System.IO.File.SetLastWriteTimeUtc ( string  path,
DateTime  lastWriteTimeUtc 
)
static

Sets the date and time, in coordinated universal time (UTC), that the specified file was last written to.

Parameters
pathThe file for which to set the date and time information.
lastWriteTimeUtcA T:System.DateTime containing the value to set for the last write date and time of path . This value is expressed in UTC time.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.FileNotFoundExceptionThe specified path was not found.
T:System.UnauthorizedAccessExceptionThe caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.ArgumentOutOfRangeExceptionlastWriteTimeUtc specifies a value outside the range of dates or times permitted for this operation.

Definition at line 806 of file File.cs.

◆ WriteAllBytes()

static void System.IO.File.WriteAllBytes ( string  path,
byte []  bytes 
)
static

Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

Parameters
pathThe file to write to.
bytesThe bytes to write to the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null or the byte array is empty.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1258 of file File.cs.

◆ WriteAllLines() [1/4]

static void System.IO.File.WriteAllLines ( string  path,
string []  contents 
)
static

Creates a new file, write the specified string array to the file, and then closes the file.

Parameters
pathThe file to write to.
contentsThe string array to write to the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionEither path or contents is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1461 of file File.cs.

◆ WriteAllLines() [2/4]

static void System.IO.File.WriteAllLines ( string  path,
string []  contents,
Encoding  encoding 
)
static

Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.

Parameters
pathThe file to write to.
contentsThe string array to write to the file.
encodingAn T:System.Text.Encoding object that represents the character encoding applied to the string array.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionEither path or contents is null.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1494 of file File.cs.

◆ WriteAllLines() [3/4]

static void System.IO.File.WriteAllLines ( string  path,
IEnumerable< string >  contents 
)
static

Creates a new file, writes a collection of strings to the file, and then closes the file.

Parameters
pathThe file to write to.
contentsThe lines to write to the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters defined by the M:System.IO.Path.GetInvalidPathChars method.
T:System.ArgumentNullExceptionEither path or contents is null.
T:System.IO.DirectoryNotFoundExceptionpath is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.IO.PathTooLongExceptionpath exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.
T:System.UnauthorizedAccessExceptionpath specifies a file that is read-only.-or-This operation is not supported on the current platform.-or- path is a directory.-or-The caller does not have the required permission.

Definition at line 1532 of file File.cs.

◆ WriteAllLines() [4/4]

static void System.IO.File.WriteAllLines ( string  path,
IEnumerable< string >  contents,
Encoding  encoding 
)
static

Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.

Parameters
pathThe file to write to.
contentsThe lines to write to the file.
encodingThe character encoding to use.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters defined by the M:System.IO.Path.GetInvalidPathChars method.
T:System.ArgumentNullExceptionEither path , contents , or encoding is null.
T:System.IO.DirectoryNotFoundExceptionpath is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.IO.PathTooLongExceptionpath exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.
T:System.UnauthorizedAccessExceptionpath specifies a file that is read-only.-or-This operation is not supported on the current platform.-or- path is a directory.-or-The caller does not have the required permission.

Definition at line 1567 of file File.cs.

◆ WriteAllText() [1/2]

static void System.IO.File.WriteAllText ( string  path,
string  contents 
)
static

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Parameters
pathThe file to write to.
contentsThe string to write to the file.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null or contents is empty.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1115 of file File.cs.

◆ WriteAllText() [2/2]

static void System.IO.File.WriteAllText ( string  path,
string  contents,
Encoding  encoding 
)
static

Creates a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten.

Parameters
pathThe file to write to.
contentsThe string to write to the file.
encodingThe encoding to apply to the string.
Exceptions
T:System.ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters as defined by F:System.IO.Path.InvalidPathChars.
T:System.ArgumentNullExceptionpath is null or contents is empty.
T:System.IO.PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
T:System.IO.DirectoryNotFoundExceptionThe specified path is invalid (for example, it is on an unmapped drive).
T:System.IO.IOExceptionAn I/O error occurred while opening the file.
T:System.UnauthorizedAccessExceptionpath specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
T:System.NotSupportedExceptionpath is in an invalid format.
T:System.Security.SecurityExceptionThe caller does not have the required permission.

Definition at line 1146 of file File.cs.


The documentation for this class was generated from the following file: