File.OpenRead(String) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Otwiera istniejący plik do odczytu.
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
Parametry
- path
- String
Plik, który ma zostać otwarty do odczytu.
Zwraca
Tylko FileStream do odczytu w określonej ścieżce.
Wyjątki
Wersje .NET Framework i .NET Core starsze niż 2.1: path to ciąg o zerowej długości, zawiera tylko biały znak lub zawiera jeden lub więcej nieprawidłowych znaków. Możesz wykonać zapytanie o nieprawidłowe znaki przy użyciu GetInvalidPathChars() metody .
Parametr path ma wartość null.
Określona ścieżka, nazwa pliku lub obie metody przekraczają maksymalną długość zdefiniowaną przez system.
Określona ścieżka jest nieprawidłowa (na przykład znajduje się na niezamapowanym dysku).
path określony katalog.
— lub —
Obiekt wywołujący nie ma wymaganych uprawnień.
Nie można odnaleźć pliku określonego w path pliku .
path jest w nieprawidłowym formacie.
Wystąpił błąd we/wy podczas otwierania pliku.
Przykłady
Poniższy przykład otwiera plik do odczytu.
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
Uwagi
Ta metoda jest równoważna FileStream(String, FileMode, FileAccess, FileShare) przeciążeniu konstruktora z wartością FileModeOpen , wartością FileAccessRead i wartością FileShareRead.
Parametr path może określać informacje o ścieżce względnej lub bezwzględnej. Informacje o ścieżce względnej są interpretowane jako względem bieżącego katalogu roboczego. Aby uzyskać bieżący katalog roboczy, zobacz GetCurrentDirectory.
Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.