File.OpenWrite(String) Metoda

Definicja

Otwiera istniejący plik lub tworzy nowy plik do zapisania.

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

Parametry

path
String

Plik, który ma zostać otwarty do zapisu.

Zwraca

Obiekt nieudostępniany FileStream na określonej ścieżce z dostępem Write .

Wyjątki

Obiekt wywołujący nie posiada wymaganych uprawnień.

-lub-

path określony plik lub katalog tylko do odczytu.

.NET Framework i .NET Core w wersjach starszych niż 2.1: path jest ciągiem o zerowej długości, zawiera tylko biały znak lub zawiera co najmniej jeden nieprawidłowy znak. Zapytania dotyczące nieprawidłowych znaków można wykonać przy użyciu GetInvalidPathChars() metody .

path to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Określona ścieżka jest nieprawidłowa (na przykład znajduje się na niemapowanym dysku).

path jest w nieprawidłowym formacie.

Przykłady

Poniższy przykład otwiera plik do odczytu i zapisu.

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";

   // Open the stream and write to it.
   {
      FileStream^ fs = File::OpenWrite( path );
      try
      {
         array<Byte>^info = (gcnew UTF8Encoding( true ))->
            GetBytes( "This is to test the OpenWrite method." );

         // 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.
   {
      FileStream^ fs = File::OpenRead( path );
      try
      {
         array<Byte>^b = gcnew array<Byte>(1024);
         UTF8Encoding^ temp = gcnew UTF8Encoding( true );
         while ( fs->Read( b, 0, b->Length ) > 0 )
         {
            Console::WriteLine( temp->GetString( b ) );
         }
      }
      finally
      {
         if ( fs )
            delete(IDisposable^)fs;
      }
   }
}
using System;
using System.IO;
using System.Text;

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

        // Open the stream and write to it.
        using (FileStream fs = File.OpenWrite(path))
        {
            Byte[] info =
                new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");

            // 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"

// Open the stream and write to it.
do
    use fs = File.OpenWrite path

    let info =
        UTF8Encoding(true)
            .GetBytes "This is to test the OpenWrite method."

    // 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"

    ' Open the stream and write to it.
    Using fs As FileStream = File.OpenWrite(path)
      Dim info As Byte() = _
       New UTF8Encoding(True).GetBytes("This is to test the OpenWrite method.")

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

    '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 przeciążeniu konstruktora FileStream(String, FileMode, FileAccess, FileShare) z trybem pliku ustawionym na OpenOrCreate, dostęp ustawiony na Writewartość , a tryb udostępniania ustawiony na Nonewartość .

Metoda OpenWrite otwiera plik, jeśli istnieje już dla ścieżki pliku lub tworzy nowy plik, jeśli jeszcze nie istnieje. W przypadku istniejącego pliku nie dołącza nowego tekstu do istniejącego tekstu. Zamiast tego zastępuje istniejące znaki nowymi znakami. Jeśli zastąpisz dłuższy ciąg (taki jak "Jest to test metody OpenWrite") z krótszym ciągiem (takim jak "Drugi przebieg"), plik będzie zawierać kombinację ciągów ("Drugi test przebiegu metody OpenWrite").

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ędne w stosunku do bieżącego katalogu roboczego. Aby uzyskać bieżący katalog roboczy, użyj GetCurrentDirectory metody .

Zwrócony FileStream element nie obsługuje odczytu. Aby otworzyć plik do odczytu i zapisu, użyj polecenia Open.

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Dotyczy

Zobacz też