File.OpenText Method
Opens an existing UTF-8 encoded text file for reading.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- path
- Type: System.String
The file to be opened for reading.
| Exception | Condition |
|---|---|
| UnauthorizedAccessException |
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 InvalidPathChars. |
| ArgumentNullException |
path is null. |
| PathTooLongException |
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. |
| DirectoryNotFoundException |
The specified path is invalid, (for example, it is on an unmapped drive). |
| FileNotFoundException |
The file specified in path was not found. |
| NotSupportedException |
path is in an invalid format. |
This method is equivalent to the StreamReader(String) constructor overload.
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 GetCurrentDirectory.
For a list of common I/O tasks, see Common I/O Tasks.
The following example opens a text file for reading.
using System; using System.IO; using System.Text; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists. if (!File.Exists(path)) { // Create the file. using (FileStream fs = File.Create(path)) { Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file."); // Add some information to the file. fs.Write(info, 0, info.Length); } } // Open the stream and read it back. using (StreamReader sr = File.OpenText(path)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } }
-
FileIOPermission
for reading from the specified file. Associated enumeration: FileIOPermissionAccess.Read
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
<#
.SYNOPSIS
This script creates a file, then reads it and displays the output.
.DESCRIPTION
This script reimplements the MSDN sample for this page. It first creates a file (assuming the file
does not already exist) and writes three lines to it. The script then opens the file, reads each
line and displays it.
.NOTES
File Name : Copy-File2.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://pshscripts.blogspot.com/2010/07/copy-file2ps1.html
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
.EXAMPLE
PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Io.File\Copy-File2.PS1'
Hello
And
Welcome
#>
# Set name of file to use for this script
$path = "c:\foo\MyTest.txt"
# Create the file if it does not already exist
if (![System.IO.File]::Exists($path)) {
$sw = [System.Io.File]::CreateText($path)
$sw.WriteLine("Hello")
$sw.WriteLine("And")
$sw.WriteLine("Welcome")
}
# Open the file to read from and set $s to an empty string
$sr = [System.Io.File]::OpenText($path)
$s = "";
# Loop through the file, line at a time and display the output
while (($s = $sr.ReadLine()) -ne $null) {
$s
}
# And close the reader/writer
$sw.Close()
$sr.Close()
- 7/18/2010
- Thomas Lee
- 7/18/2010
- Thomas Lee