File Class

Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.

Syntax

Declaration

[ComVisible(true)]
public static class File

Inheritance Hierarchy

Inherited Members

System.Object

, , , , , ,

Methods summary

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.

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.

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.

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

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

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

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

Creates or overwrites a file in the specified path.

Creates or overwrites the specified file.

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

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

Deletes the specified file.

Determines whether the specified file exists.

Gets the FileAttributes of the file on the path.

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

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

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

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

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

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

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

Opens a FileStream on the specified path with read/write access.

Opens a FileStream on the specified path, with the specified mode and access.

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

Opens an existing file for reading.

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

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

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

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

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

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

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

Reads the lines of a file.

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

Sets the specified FileAttributes of the file on the specified path.

Sets the date and time the file was created.

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

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

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

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

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

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.

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

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

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.

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.

Methods

  • AppendAllLines(String, IEnumerable<String>)

    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.

    public static void AppendAllLines(string path, IEnumerable<string> contents)

    Parameters

    • path

      The file to append the lines to. The file is created if it doesn't already exist.

    • contents

      The lines to append to the file.

    Exceptions

  • AppendAllLines(String, IEnumerable<String>, 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.

    public static void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)

    Parameters

    • path

      The file to append the lines to. The file is created if it doesn't already exist.

    • contents

      The lines to append to the file.

    • encoding

      The character encoding to use.

    Exceptions

  • AppendAllText(String, String)

    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.

    public static void AppendAllText(string path, string contents)

    Parameters

    • path

      The file to append the specified string to.

    • contents

      The string to append to the file.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

    • path is null.

    • The 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.

    • The specified path is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).

    • An I/O error occurred while opening the file.

    • path 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.

    • path is in an invalid format.

    • The caller does not have the required permission.

  • AppendAllText(String, String, Encoding)

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

    public static void AppendAllText(string path, string contents, Encoding encoding)

    Parameters

    • path

      The file to append the specified string to.

    • contents

      The string to append to the file.

    • encoding

      The character encoding to use.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

    • path is null.

    • The 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.

    • The specified path is invalid (for example, the directory doesn’t exist or it is on an unmapped drive).

    • An I/O error occurred while opening the file.

    • path 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.

    • path is in an invalid format.

    • The caller does not have the required permission.

  • AppendText(String)

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

    public static StreamWriter AppendText(string path)

    Parameters

    • path

      The 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

  • Copy(String, String)

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

    public static void Copy(string sourceFileName, string destFileName)

    Parameters

    • sourceFileName

      The file to copy.

    • destFileName

      The name of the destination file. This cannot be a directory or an existing file.

    Exceptions

    • The caller does not have the required permission.

    • sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

      -or-

      sourceFileName or destFileName specifies a directory.

    • sourceFileName or destFileName is null.

    • The 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.

    • The path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive).

    • sourceFileName was not found.

    • destFileName exists.

      -or-

      An I/O error has occurred.

    • sourceFileName or destFileName is in an invalid format.

  • Copy(String, String, Boolean)

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

    public static void Copy(string sourceFileName, string destFileName, bool overwrite)

    Parameters

    • sourceFileName

      The file to copy.

    • destFileName

      The name of the destination file. This cannot be a directory.

    • overwrite

      true if the destination file can be overwritten; otherwise, false.

    Exceptions

    • The caller does not have the required permission.

      -or-

      destFileName is read-only.

    • sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

      -or-

      sourceFileName or destFileName specifies a directory.

    • sourceFileName or destFileName is null.

    • The 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.

    • The path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive).

    • sourceFileName was not found.

    • destFileName exists and overwrite is false.

      -or-

      An I/O error has occurred.

    • sourceFileName or destFileName is in an invalid format.

  • Create(String)

    Creates or overwrites a file in the specified path.

    public static FileStream Create(string path)

    Parameters

    • path

      The path and name of the file to create.

    Returns

    Exceptions

  • Create(String, Int32)

    Creates or overwrites the specified file.

    public static FileStream Create(string path, int bufferSize)

    Parameters

    • path

      The name of the file.

    • bufferSize

      The number of bytes buffered for reads and writes to the file.

    Returns

    • A FileStream with the specified buffer size that provides read/write access to the file specified in path.

    Exceptions

  • Create(String, Int32, FileOptions)

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

    public static FileStream Create(string path, int bufferSize, FileOptions options)

    Parameters

    • path

      The name of the file.

    • bufferSize

      The number of bytes buffered for reads and writes to the file.

    • options

      One of the FileOptions values that describes how to create or overwrite the file.

    Returns

    • A new file with the specified buffer size.

    Exceptions

  • CreateText(String)

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

    public static StreamWriter CreateText(string path)

    Parameters

    • path

      The file to be opened for writing.

    Returns

    Exceptions

  • Delete(String)

    Deletes the specified file.

    public static void Delete(string path)

    Parameters

    • path

      The name of the file to be deleted. Wildcard characters are not supported.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

    • path is null.

    • The specified path is invalid (for example, it is on an unmapped drive).

    • The 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.

    • path is in an invalid format.

    • The 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.

    • The 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.

  • Exists(String)

    Determines whether the specified file exists.

    public static bool Exists(string path)

    Parameters

    • path

      The 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.

  • GetAttributes(String)

    Gets the FileAttributes of the file on the path.

    public static FileAttributes GetAttributes(string path)

    Parameters

    • path

      The path to the file.

    Returns

    Exceptions

    • path is empty, contains only white spaces, or contains invalid characters.

    • The 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.

    • path is in an invalid format.

    • path represents a file and is invalid, such as being on an unmapped drive, or the file cannot be found.

    • path represents a directory and is invalid, such as being on an unmapped drive, or the directory cannot be found.

    • This file is being used by another process.

    • The caller does not have the required permission.

  • GetCreationTime(String)

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

    public static DateTime GetCreationTime(string path)

    Parameters

    • path

      The file or directory for which to obtain creation date and time information.

    Returns

    • A DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in local time.

    Exceptions

  • GetCreationTimeUtc(String)

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

    public static DateTime GetCreationTimeUtc(string path)

    Parameters

    • path

      The file or directory for which to obtain creation date and time information.

    Returns

    • A DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in UTC time.

    Exceptions

  • GetLastAccessTime(String)

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

    public static DateTime GetLastAccessTime(string path)

    Parameters

    • path

      The file or directory for which to obtain access date and time information.

    Returns

    • A 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

  • GetLastAccessTimeUtc(String)

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

    public static DateTime GetLastAccessTimeUtc(string path)

    Parameters

    • path

      The file or directory for which to obtain access date and time information.

    Returns

    • A 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

  • GetLastWriteTime(String)

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

    public static DateTime GetLastWriteTime(string path)

    Parameters

    • path

      The file or directory for which to obtain write date and time information.

    Returns

    • A 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

  • GetLastWriteTimeUtc(String)

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

    public static DateTime GetLastWriteTimeUtc(string path)

    Parameters

    • path

      The file or directory for which to obtain write date and time information.

    Returns

    • A 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

  • Move(String, String)

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

    public static void Move(string sourceFileName, string destFileName)

    Parameters

    • sourceFileName

      The name of the file to move. Can include a relative or absolute path.

    • destFileName

      The new path and name for the file.

    Exceptions

    • The destination file already exists.

      -or-

      sourceFileName was not found.

    • sourceFileName or destFileName is null.

    • sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters as defined in InvalidPathChars.

    • The caller does not have the required permission.

    • The 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.

    • The path specified in sourceFileName or destFileName is invalid, (for example, it is on an unmapped drive).

    • sourceFileName or destFileName is in an invalid format.

  • Open(String, FileMode)

    Opens a FileStream on the specified path with read/write access.

    public static FileStream Open(string path, FileMode mode)

    Parameters

    • path

      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.

    Returns

    • A FileStream opened in the specified mode and path, with read/write access and not shared.

    Exceptions

  • Open(String, FileMode, FileAccess)

    Opens a FileStream on the specified path, with the specified mode and access.

    public static FileStream Open(string path, FileMode mode, FileAccess access)

    Parameters

    • path

      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.

    Returns

    • An unshared FileStream that provides access to the specified file, with the specified mode and access.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

      -or-

      access specified Read and mode specified Create, CreateNew, Truncate, or Append.

    • path is null.

    • The 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.

    • The specified path is invalid, (for example, it is on an unmapped drive).

    • An I/O error occurred while opening the file.

    • path 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 Create and the specified file is a hidden file.

    • mode or access specified an invalid value.

    • The file specified in path was not found.

    • path is in an invalid format.

  • Open(String, FileMode, FileAccess, FileShare)

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

    public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)

    Parameters

    • path

      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.

    Returns

    • A FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

      -or-

      access specified Read and mode specified Create, CreateNew, Truncate, or Append.

    • path is null.

    • The 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.

    • The specified path is invalid, (for example, it is on an unmapped drive).

    • An I/O error occurred while opening the file.

    • path 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 Create and the specified file is a hidden file.

    • mode, access, or share specified an invalid value.

    • The file specified in path was not found.

    • path is in an invalid format.

  • OpenRead(String)

    Opens an existing file for reading.

    public static FileStream OpenRead(string path)

    Parameters

    • path

      The file to be opened for reading.

    Returns

    Exceptions

  • OpenText(String)

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

    public static StreamReader OpenText(string path)

    Parameters

    • path

      The file to be opened for reading.

    Returns

    Exceptions

  • OpenWrite(String)

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

    public static FileStream OpenWrite(string path)

    Parameters

    • path

      The file to be opened for writing.

    Returns

    Exceptions

  • ReadAllBytes(String)

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

    public static byte[] ReadAllBytes(string path)

    Parameters

    • path

      The file to open for reading.

    Returns

    • A byte array containing the contents of the file.

    Exceptions

  • ReadAllLines(String)

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

    public static string[] ReadAllLines(string path)

    Parameters

    • path

      The file to open for reading.

    Returns

    • A string array containing all lines of the file.

    Exceptions

  • ReadAllLines(String, Encoding)

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

    public static string[] ReadAllLines(string path, Encoding encoding)

    Parameters

    • path

      The file to open for reading.

    • encoding

      The encoding applied to the contents of the file.

    Returns

    • A string array containing all lines of the file.

    Exceptions

  • ReadAllText(String)

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

    public static string ReadAllText(string path)

    Parameters

    • path

      The file to open for reading.

    Returns

    • A string containing all lines of the file.

    Exceptions

  • ReadAllText(String, Encoding)

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

    public static string ReadAllText(string path, Encoding encoding)

    Parameters

    • path

      The file to open for reading.

    • encoding

      The encoding applied to the contents of the file.

    Returns

    • A string containing all lines of the file.

    Exceptions

  • ReadLines(String)

    Reads the lines of a file.

    public static IEnumerable<string> ReadLines(string path)

    Parameters

    • path

      The file to read.

    Returns

    • All the lines of the file, or the lines that are the result of a query.

    Exceptions

  • ReadLines(String, Encoding)

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

    public static IEnumerable<string> ReadLines(string path, Encoding encoding)

    Parameters

    • path

      The file to read.

    • encoding

      The 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

  • SetAttributes(String, FileAttributes)

    Sets the specified FileAttributes of the file on the specified path.

    [SecurityCritical]
    public static void SetAttributes(string path, FileAttributes fileAttributes)

    Parameters

    • path

      The path to the file.

    • fileAttributes

      A bitwise combination of the enumeration values.

    Exceptions

    • path is empty, contains only white spaces, contains invalid characters, or the file attribute is invalid.

    • The 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.

    • path is in an invalid format.

    • The specified path is invalid, (for example, it is on an unmapped drive).

    • The file cannot be found.

    • path 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.

  • SetCreationTime(String, DateTime)

    Sets the date and time the file was created.

    public static void SetCreationTime(string path, DateTime creationTime)

    Parameters

    • path

      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.

    Exceptions

  • SetCreationTimeUtc(String, DateTime)

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

    public static void SetCreationTimeUtc(string path, DateTime creationTimeUtc)

    Parameters

    • path

      The file for which to set the creation date and time information.

    • creationTimeUtc

      A DateTime containing the value to set for the creation date and time of path. This value is expressed in UTC time.

    Exceptions

  • SetLastAccessTime(String, DateTime)

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

    public static void SetLastAccessTime(string path, DateTime lastAccessTime)

    Parameters

    • path

      The file for which to set the access date and time information.

    • lastAccessTime

      A DateTime containing the value to set for the last access date and time of path. This value is expressed in local time.

    Exceptions

  • SetLastAccessTimeUtc(String, DateTime)

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

    public static void SetLastAccessTimeUtc(string path, DateTime lastAccessTimeUtc)

    Parameters

    • path

      The file for which to set the access date and time information.

    • lastAccessTimeUtc

      A DateTime containing the value to set for the last access date and time of path. This value is expressed in UTC time.

    Exceptions

  • SetLastWriteTime(String, DateTime)

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

    public static void SetLastWriteTime(string path, DateTime lastWriteTime)

    Parameters

    • path

      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.

    Exceptions

  • SetLastWriteTimeUtc(String, DateTime)

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

    public static void SetLastWriteTimeUtc(string path, DateTime lastWriteTimeUtc)

    Parameters

    • path

      The file for which to set the date and time information.

    • lastWriteTimeUtc

      A DateTime containing the value to set for the last write date and time of path. This value is expressed in UTC time.

    Exceptions

  • WriteAllBytes(String, Byte[])

    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.

    public static void WriteAllBytes(string path, byte[] bytes)

    Parameters

    • path

      The file to write to.

    • bytes

      The bytes to write to the file.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

    • path is null or the byte array is empty.

    • The 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.

    • The specified path is invalid (for example, it is on an unmapped drive).

    • An I/O error occurred while opening the file.

    • path 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.

    • path is in an invalid format.

    • The caller does not have the required permission.

  • WriteAllLines(String, IEnumerable<String>)

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

    public static void WriteAllLines(string path, IEnumerable<string> contents)

    Parameters

    Exceptions

  • WriteAllLines(String, IEnumerable<String>, Encoding)

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

    public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding)

    Parameters

    Exceptions

  • WriteAllText(String, String)

    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.

    public static void WriteAllText(string path, string contents)

    Parameters

    • path

      The file to write to.

    • contents

      The string to write to the file.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

    • path is null or contents is empty.

    • The 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.

    • The specified path is invalid (for example, it is on an unmapped drive).

    • An I/O error occurred while opening the file.

    • path 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.

    • path is in an invalid format.

    • The caller does not have the required permission.

  • WriteAllText(String, String, 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.

    public static void WriteAllText(string path, string contents, Encoding encoding)

    Parameters

    • path

      The file to write to.

    • contents

      The string to write to the file.

    • encoding

      The encoding to apply to the string.

    Exceptions

    • path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

    • path is null or contents is empty.

    • The 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.

    • The specified path is invalid (for example, it is on an unmapped drive).

    • An I/O error occurred while opening the file.

    • path 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.

    • path is in an invalid format.

    • The caller does not have the required permission.

Details

Namespace

System.IO

Assembly

System.IO.FileSystem.dll

See Also