File.OpenRead(String) Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Okumak için var olan bir dosyayı açar.
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
Parametreler
- path
- String
Okunmak üzere açılacak dosya.
Döndürülenler
Belirtilen yolda salt FileStream okunur.
Özel durumlar
2.1'den eski .NET Framework ve .NET Core sürümleri: path sıfır uzunlukta bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir.
GetInvalidPathChars() yöntemini kullanarak geçersiz karakterleri sorgulayabilirsiniz.
path, null'e eşittir.
Belirtilen yol, dosya adı veya her ikisi de sistem tanımlı uzunluk üst sınırını aşıyor.
Belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).
içinde path belirtilen dosya bulunamadı.
path geçersiz biçimde.
Dosya açılırken bir G/Ç hatası oluştu.
Örnekler
Aşağıdaki örnek, okumak üzere bir dosya açar.
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
Açıklamalar
Bu yöntem, değeri ve değeri FileStream(String, FileMode, FileAccess, FileShare) olan oluşturucu aşırı yüklemesine FileMode eşdeğerdir OpenFileAccess.ReadFileShareRead
parametresinin path göreli veya mutlak yol bilgilerini belirtmesine izin verilir. Göreli yol bilgileri geçerli çalışma dizinine göre yorumlanır. Geçerli çalışma dizinini edinmek için bkz. GetCurrentDirectory.
Yaygın G/Ç görevlerinin listesi için bkz. Ortak G/Ç Görevleri.