File.OpenRead(String) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
읽기 위해 기존 파일을 엽니다.
public:
static System::IO::FileStream ^ OpenRead(System::String ^ path);
public static System.IO.FileStream OpenRead(string path);
static member OpenRead : string -> System.IO.FileStream
Public Shared Function OpenRead (path As String) As FileStream
매개 변수
- path
- String
읽기 위해 열 파일입니다.
반품
지정된 경로의 읽기 전용 FileStream 입니다.
예외
.NET Framework 및 .NET Core 버전 2.1 이전: path 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. 메서드를 사용하여 잘못된 문자를 쿼리할 GetInvalidPathChars() 수 있습니다.
path은 null입니다.
지정된 경로, 파일 이름 또는 둘 다 시스템 정의 최대 길이를 초과합니다.
지정한 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있는 경우).
지정한 path 파일을 찾을 수 없습니다.
path 가 잘못된 형식입니다.
파일을 여는 동안 I/O 오류가 발생했습니다.
예제
다음은 읽기용 파일을 여는 예제입니다.
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 (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
open System.IO
open System.Text
let path = @"c:\temp\MyTest.txt"
if File.Exists path |> not then
// Create the file.
use fs = File.Create path
let info =
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.
do
use fs = File.OpenRead path
let b = Array.zeroCreate 1024
let temp = UTF8Encoding true
while fs.Read(b, 0, b.Length) > 0 do
printfn $"{temp.GetString b}"
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 fs As FileStream = File.OpenRead(path)
Dim b(1023) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Do While fs.Read(b, 0, b.Length) > 0
Console.WriteLine(temp.GetString(b))
Loop
End Using
End Sub
End Class
설명
이 메서드는 값, FileStream(String, FileMode, FileAccess, FileShare)FileMode 값 및 값OpenFileAccess이 있는 생성자 오버로드 Read 와 FileShare 동일합니다Read.
path 매개 변수는 상대 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 다음을 참조하세요 GetCurrentDirectory.
일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.