System.IO.File Class

Assembly: Mscorlib.dll
Namespace: System.IO
Summary
Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.
C# Syntax:
public sealed class File
Remarks
All methods of the File class are static and can therefore be called without having an instance of a file. The FileInfo class contains only instance methods. The static methods of the File class perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of FileInfo instead, because the security check will not always be necessary.

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.


Note In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.

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:

See also:
System.IO Namespace

System.IO.File Member List:

Public Methods
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.
Protected Methods
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:


System.IO.File Member Details

Method: AppendText(
   string path
)
Summary
Creates a StreamWriter that appends UTF-8 encoded text to an existing file.
C# Syntax:
public static StreamWriter AppendText(
   string path
);
Parameters:

path

The path to the file to append to.

Return Value:
A StreamWriter that appends UTF-8 encoded text to an existing file.
Exceptions
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.
Remarks
This method is equivalent to StreamWriter. If the file specified by path does not exist, it is created. If the file does exist, write operations to the StreamWriter append text to the file. Additional threads are permitted to read the file while it is open.

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.

Example
This example appends text from an existing file to another existing file.
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();
   }
}

    
.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write
See also:
StreamWriter

Return to top


Overloaded Method: Copy(
   string sourceFileName,
   string destFileName
)
Summary
Copies an existing file to a new file. Overwriting a file of the same name is not allowed.
C# Syntax:
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
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.
Remarks
This method is equivalent to Copy.

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.

.NET Framework Security:
FileIOPermission for reading from sourceFileName and writing to destFileName. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded Method: Copy(
   string sourceFileName,
   string destFileName,
   bool overwrite
)
Summary
Copies an existing file to a new file. Overwriting a file of the same name is allowed.
C# Syntax:
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 or an existing file.

overwrite

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

Exceptions
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for reading from sourceFileName and writing to destFileName. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded Method: Create(
   string path
)
Summary
Creates a file in the specified fully qualified path.
C# Syntax:
public static FileStream Create(
   string path
);
Parameters:

path

The fully qualified path and name of the file to create.

Return Value:
A FileStream that provides read/write access to the specified file.
Exceptions
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.
Remarks
This method is equivalent to Create using the default buffer size.

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.

.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Overloaded Method: Create(
   string path,
   int bufferSize
)
Summary
Creates or overwrites the specified file.
C# Syntax:
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.

Return Value:
A new file with the specified buffer size.
Exceptions
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.
Remarks
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.

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.

.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumerations: FileIOPermissionAccess.Write

Return to top


Method: CreateText(
   string path
)
Summary
Creates or opens a new file for writing UTF-8 encoded text.
C# Syntax:
public static StreamWriter CreateText(
   string path
);
Parameters:

path

The file to be opened for writing.

Return Value:
A StreamWriter that writes to the specified file using UTF-8 encoding.
Exceptions
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.
Remarks
This method is equivalent to StreamWriter. If the file specified by path does not exist, it is created. If the file does exist, its contents are overwritten. Additional threads are permitted to read the file while it is open.

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

.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: Delete(
   string path
)
Summary
Deletes the file specified by the fully qualified path. An exception is not thrown if the specified file does not exist.
C# Syntax:
public static void Delete(
   string path
);
Parameters:

path

The name of the file to be deleted.

Exceptions
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Exists(
   string path
)
Summary
Determines whether the specified file exists.
C# Syntax:
public static bool Exists(
   string path
);
Parameters:

path

The file to check.

Return Value:
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 or a zero-length string.
Exceptions
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.
Remarks
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.

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.

.NET Framework Security:
FileIOPermission for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~File();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetAttributes(
   string path
)
Summary
Gets the FileAttributes of the file on the fully qualified path.
C# Syntax:
public static FileAttributes GetAttributes(
   string path
);
Parameters:

path

The path to the file.

Return Value:
The FileAttributes of the file on the fully qualified path, or -1 if the path or file is not found.
Exceptions
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for reading files. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Method: GetCreationTime(
   string path
)
Summary
Returns the creation date and time of the specified file or directory.
C# Syntax:
public static DateTime GetCreationTime(
   string path
);
Parameters:

path

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

Return Value:
A DateTime structure set to the creation date and time for the specified file or directory. This value is expressed in local time.
Exceptions
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetLastAccessTime(
   string path
)
Summary
Returns the date and time the specified file or directory was last accessed.
C# Syntax:
public static DateTime GetLastAccessTime(
   string path
);
Parameters:

path

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

Return Value:
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
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Method: GetLastWriteTime(
   string path
)
Summary
Returns the date and time the specified file or directory was last written to.
C# Syntax:
public static DateTime GetLastWriteTime(
   string path
);
Parameters:

path

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

Return Value:
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
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Move(
   string sourceFileName,
   string destFileName
)
Summary
Moves a specified file to a new location, providing the option to specify a new file name.
C# Syntax:
public static void Move(
   string sourceFileName,
   string destFileName
);
Parameters:

sourceFileName

The name of the file to move.

destFileName

The new path for the file.

Exceptions
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.
Remarks
This method works across disk volumes, and it does not throw an exception if the source and destination are the same.

The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.

.NET Framework Security:
FileIOPermission for reading from sourceFileName and writing to destFileName. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded Method: Open(
   string path,
   FileMode mode
)
Summary
Opens a FileStream on the specified path with read/write access.
C# Syntax:
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.

Return Value:
A FileStream opened in the specified mode and path, with read/write access and not shared.
Exceptions
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.
Remarks
This method is equivalent to FileStream.

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.

.NET Framework Security:
FileIOPermission for reading from and writing to the specified file. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded Method: Open(
   string path,
   FileMode mode,
   FileAccess access
)
Summary
Opens a FileStream on the specified fully qualified path, with the specified mode and access.
C# Syntax:
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.

Return Value:
An unshared FileStream that provides access to the specified file, with the specified mode and access.
Exceptions
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.
Remarks
This method is equivalent to FileStream.

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.

.NET Framework Security:
FileIOPermission for reading from and writing to the specified file. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Overloaded Method: Open(
   string path,
   FileMode mode,
   FileAccess access,
   FileShare share
)
Summary
Opens a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.
C# Syntax:
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.

Return Value:
A FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.
Exceptions
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for reading from and writing to the specified file. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write

Return to top


Method: OpenRead(
   string path
)
Summary
Opens an existing file for reading.
C# Syntax:
public static FileStream OpenRead(
   string path
);
Parameters:

path

The file to be opened for reading.

Return Value:
A read-only FileStream on the specified path.
Exceptions
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.
Remarks
This method is equivalent to FileStream.

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.

.NET Framework Security:
FileIOPermission for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Method: OpenText(
   string path
)
Summary
Opens an existing UTF-8 encoded text file for reading.
C# Syntax:
public static StreamReader OpenText(
   string path
);
Parameters:

path

The file to be opened for reading.

Return Value:
A StreamReader on the specified path.
Exceptions
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.
Remarks
This method is equivalent to StreamReader.

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.

.NET Framework Security:
FileIOPermission for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read

Return to top


Method: OpenWrite(
   string path
)
Summary
Opens an existing file for writing.
C# Syntax:
public static FileStream OpenWrite(
   string path
);
Parameters:

path

The file to be opened for writing.

Return Value:
A read/write, unshared FileStream object on the specified path.
Exceptions
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.
Remarks
This method is equivalent to FileStream.

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.

.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: SetAttributes(
   string path,
   FileAttributes fileAttributes
)
Summary
Sets the specified FileAttributes of the file on the specified path.
C# Syntax:
public static void SetAttributes(
   string path,
   FileAttributes fileAttributes
);
Parameters:

path

The fully qualified path to the file.

fileAttributes

The desired FileAttributes, such as Hidden, ReadOnly, Normal, and Archive.

Exceptions
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for reading and writing files. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: SetCreationTime(
   string path,
   DateTime creationTime
)
Summary
Sets the date and time the file was created.
C# Syntax:
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
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: SetLastAccessTime(
   string path,
   DateTime lastAccessTime
)
Summary
Sets the date and time the specified file was last accessed.
C# Syntax:
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
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: SetLastWriteTime(
   string path,
   DateTime lastWriteTime
)
Summary
Sets the date and time that the specified file was last written to.
C# Syntax:
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
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.
Remarks
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.
.NET Framework Security:
FileIOPermission for writing to the specified file. Associated enumeration: FileIOPermissionAccess.Write

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.