public sealed class File
|
By default, full read/write access to new files is granted to all users.
The following table describes the enumerations that are used to customize the behavior of various File methods.
Enumeration | Description |
---|---|
FileAccess | Specifies read and write access to a file. |
FileShare | Specifies the level of access permitted for a file that is already in use. |
FileMode | Specifies whether the contents of an existing file are preserved or overwritten, and whether requests to create an existing file cause an exception. |
In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all of the following are acceptable paths:
AppendText | Creates a StreamWriter that appends UTF-8 encoded text to an existing file. |
Copy | Overloaded:Copy(string sourceFileName, string destFileName) Copies an existing file to a new file. Overwriting a file of the same name is not allowed. |
Copy | Overloaded:Copy(string sourceFileName, string destFileName, bool overwrite) Copies an existing file to a new file. Overwriting a file of the same name is allowed. |
Create | Overloaded:Create(string path) Creates a file in the specified fully qualified path. |
Create | Overloaded:Create(string path, int bufferSize) Creates or overwrites the specified file. |
CreateText | Creates or opens a new file for writing UTF-8 encoded text. |
Delete | Deletes the file specified by the fully qualified path. An exception is not thrown if the specified file does not exist. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
Exists | Determines whether the specified file exists. |
GetAttributes | Gets the FileAttributes of the file on the fully qualified path. |
GetCreationTime | Returns the creation date and time of the specified file or directory. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLastAccessTime | Returns the date and time the specified file or directory was last accessed. |
GetLastWriteTime | Returns the date and time the specified file or directory was last written to. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Move | Moves a specified file to a new location, providing the option to specify a new file name. |
Open | Overloaded:Open(string path, FileMode mode) Opens a FileStream on the specified path with read/write access. |
Open | Overloaded:Open(string path, FileMode mode, FileAccess access) Opens a FileStream on the specified fully qualified path, with the specified mode and access. |
Open | Overloaded:Open(string path, FileMode mode, FileAccess access, FileShare share) Opens a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. |
OpenRead | Opens an existing file for reading. |
OpenText | Opens an existing UTF-8 encoded text file for reading. |
OpenWrite | Opens an existing file for writing. |
SetAttributes | Sets the specified FileAttributes of the file on the specified path. |
SetCreationTime | Sets the date and time the file was created. |
SetLastAccessTime | Sets the date and time the specified file was last accessed. |
SetLastWriteTime | Sets the date and time that the specified file was last written to. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public static StreamWriter AppendText( |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
FileNotFoundException | The file specified in path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
using System; using System.IO; class TestAppend { public static void Main(String[] args) { // Append the text from MyFile.txt to this file. TextWriter output = File.AppendText("c:\\NewFile.txt"); // Append text from this file to NewFile.txt. TextReader input = File.OpenText("c:\\MyFile.txt"); String line; while((line = input.ReadLine())!= null) output.WriteLine(line); input.Close(); output.Close(); } }
sourceFileName
destFileName
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. -or- sourceFileName or destFileName specifies a directory. |
ArgumentNullException | sourceFileName or destFileName is null. |
PathTooLongException | The length or the absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length. |
DirectoryNotFoundException | Directory information specified in sourceFileName or destFileName was not found. |
FileNotFoundException | sourceFileName was not found. |
IOException | destFileName exists. -or- An I/O error has occurred. |
NotSupportedException | sourceFileName or destFileName contains a colon (:) in the middle of the string. |
The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
sourceFileName
destFileName
overwrite
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. -or- sourceFileName or destFileName specifies a directory. |
ArgumentNullException | sourceFileName or destFileName is null. |
PathTooLongException | The length or the absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length. |
DirectoryNotFoundException | Directory information specified in sourceFileName or destFileName was not found. |
FileNotFoundException | sourceFileName was not found. |
IOException | destFileName is read-only, or destFileName exists and overwrite is false. -or- An I/O error has occurred. |
NotSupportedException | sourceFileName or destFileName contains a colon (:) in the middle of the string. |
public static FileStream Create( |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
IOException | An I/O error occurred while creating the file. |
UnauthorizedAccessException | path specified a file that is read-only. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
If the specified file does not exist, it is created; if it does exist and it is not read-only, the contents are overwritten.
By default, full read/write access to new files is granted to all users. The file is opened with read/write access and must be closed before it can be opened by another application.
public static FileStream Create( |
path
bufferSize
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
IOException | An I/O error occurred while creating the file. |
UnauthorizedAccessException | path specified a file that is read-only. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
This method is equivalent to FileStream. If the specified file does not exist, it is created; if it does exist and it is not read-only, the contents are overwritten.
By default, full read/write access to new files is granted to all users. The file is opened with read/write access and must be closed before it can be opened by another application.
public static StreamWriter CreateText( |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
public static void Delete( |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
IOException | The specified file is in use. |
UnauthorizedAccessException | path is a directory. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
path
Exception Type | Condition |
---|---|
ArgumentException | path is empty, contains only white spaces, or contains invalid characters as specified by Path.InvalidPathChars. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
ArgumentNullException | path is null. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
If path describes a directory, this method returns false.
~File(); |
public static FileAttributes GetAttributes( |
path
Exception Type | Condition |
---|---|
ArgumentException | path is empty, contains only white spaces, or contains invalid characters. |
PathTooLongException | The specified path, file name, or both are too long. After full qualification, each must contain fewer than 256 characters. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
DirectoryNotFoundException | The specified directory does not exist. |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
IOException | path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
public virtual int GetHashCode(); |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
IOException | The specified path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
IOException | The specified path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
public Type GetType(); |
protected object MemberwiseClone(); |
sourceFileName
destFileName
Exception Type | Condition |
---|---|
IOException | The destination file is read-only. |
ArgumentNullException | sourceFileName or destFileName is null. |
ArgumentException | sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters as defined in Path.InvalidPathChars. |
SecurityException | The caller does not have the required permission. |
UnauthorizedAccessException | destFileName is a directory. |
FileNotFoundException | sourceFileName was not found. |
PathTooLongException | The length or the absolute path information for sourceFileName or destFileName exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory information in sourceFileName or destFileName was not found. |
NotSupportedException | sourceFileName or destFileName contains a colon (:) in the middle of the string. |
The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
public static FileStream Open( |
path
mode
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
IOException | The file already exists. |
UnauthorizedAccessException | path specified a file that is read-only. -or- This operation is not supported on the current platform. -or- path specified a directory. |
ArgumentOutOfRangeException | mode specified an invalid value. |
FileNotFoundException | The file specified in path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
public static FileStream Open( |
path
mode
access
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. -or- access specified Read and mode specified Create, CreateNew, Truncate, or Append. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
IOException | The file already exists. |
UnauthorizedAccessException | path specified a file that is read-only and access is not Read. -or- path specified a directory. |
ArgumentOutOfRangeException | mode or access specified an invalid value. |
FileNotFoundException | The file specified in path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
public static FileStream Open( |
path
mode
access
share
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. -or- access specified Read and mode specified Create, CreateNew, Truncate, or Append. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
IOException | The file already exists. |
UnauthorizedAccessException | path specified a file that is read-only and access is not Read. -or- path specified a directory. |
ArgumentOutOfRangeException | mode, access, or share specified an invalid value. |
FileNotFoundException | The file specified in path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
public static FileStream OpenRead( |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
UnauthorizedAccessException | path specified a file that is read-only. -or- path specified a directory. |
FileNotFoundException | The file specified in path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
public static StreamReader OpenText( |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
FileNotFoundException | The file specified in path was not found. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
public static FileStream OpenWrite( |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The directory specified in path was not found. |
FileNotFoundException | The file specified in path was not found. |
UnauthorizedAccessException | path specified a read-only file or directory. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
public static void SetAttributes( |
path
fileAttributes
Exception Type | Condition |
---|---|
ArgumentException | path is empty, contains only white spaces, contains invalid characters, or the file attribute is invalid. |
PathTooLongException | The specified path, file name, or both are too long. After full qualification, each must contain fewer than 256 characters. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
DirectoryNotFoundException | The specified directory does not exist. |
path
creationTime
Exception Type | Condition |
---|---|
FileNotFoundException | The specified path was not found. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
IOException | The specified path was not found. |
ArgumentOutOfRangeException | creationTime specifies a value outside the range of dates, times, or both permitted for this operation. |
SecurityException | The caller does not have the required permission. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
path
lastAccessTime
Exception Type | Condition |
---|---|
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
IOException | The specified path was not found. |
SecurityException | The caller does not have the required permission. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
path
lastWriteTime
Exception Type | Condition |
---|---|
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
IOException | The specified path was not found. |
SecurityException | The caller does not have the required permission. |
NotSupportedException | path contains a colon (:) in the middle of the string. |
public virtual string ToString(); |