File.OpenText(String) File.OpenText(String) File.OpenText(String) File.OpenText(String) Method

定义

打开现有 UTF-8 编码文本文件以进行读取。Opens an existing UTF-8 encoded text file for reading.

public:
 static System::IO::StreamReader ^ OpenText(System::String ^ path);
public static System.IO.StreamReader OpenText (string path);
static member OpenText : string -> System.IO.StreamReader
Public Shared Function OpenText (path As String) As StreamReader

参数

path
String String String String

要打开以进行读取的文件。The file to be opened for reading.

返回

指定路径上的 StreamReaderA StreamReader on the specified path.

异常

调用方没有所要求的权限。The caller does not have the required permission.

path 是一个长度为零的字符串,仅包含空格,或包含一个或多个由 InvalidPathChars 定义的无效字符。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的路径和/或文件名超过了系统定义的最大长度。The specified path, file name, or both exceed the system-defined maximum length.

指定的路径无效(例如,它位于未映射的驱动器上)。The specified path is invalid, (for example, it is on an unmapped drive).

未找到 path 中指定的文件。The file specified in path was not found.

path 的格式无效。path is in an invalid format.

示例

下面的示例打开一个文本文件以进行读取。The following example opens a text file for reading.

using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   if ( !File::Exists( path ) )
   {
      // Create the file.
      FileStream^ fs = File::Create( path );
      try
      {
         array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( "This is some text in the file." );
         
         // Add some information to the file.
         fs->Write( info, 0, info->Length );
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }
   
   // Open the stream and read it back.
   StreamReader^ sr = File::OpenText( path );
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
         delete (IDisposable^)sr;
   }
}
using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        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);
            }
        }
    }
}
Imports System.IO
Imports System.Text

Public Class Test
  Public Shared Sub Main()
    Dim path As String = "c:\temp\MyTest.txt"

    If Not File.Exists(path) Then
      ' Create the file.
      Using fs As FileStream = File.Create(path)
        Dim info As Byte() = _
         New UTF8Encoding(True).GetBytes("This is some text in the file.")

        ' Add some information to the file.
        fs.Write(info, 0, info.Length)
      End Using
    End If

    ' Open the stream and read it back. 
    Using sr As StreamReader = File.OpenText(path)
      Do While sr.Peek() >= 0
        Console.WriteLine(sr.ReadLine())
      Loop
    End Using

  End Sub
End Class

注解

此方法等效于StreamReader(String)构造函数重载。This method is equivalent to the StreamReader(String) constructor overload.

允许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. 若要获取当前工作目录,请GetCurrentDirectory参见。To obtain the current working directory, see GetCurrentDirectory.

有关常见 i/o 任务的列表, 请参阅常见 I/o 任务For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
用于从指定的文件中读取。for reading from the specified file. 关联的枚举:ReadAssociated enumeration: Read

适用于

另请参阅