Freigeben über


File.OpenWrite-Methode

Öffnet eine vorhandene Datei zum Schreiben.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function OpenWrite ( _
    path As String _
) As FileStream
'Usage
Dim path As String
Dim returnValue As FileStream

returnValue = File.OpenWrite(path)
public static FileStream OpenWrite (
    string path
)
public:
static FileStream^ OpenWrite (
    String^ path
)
public static FileStream OpenWrite (
    String path
)
public static function OpenWrite (
    path : String
) : FileStream

Parameter

  • path
    Die Datei, die zum Schreiben geöffnet werden soll.

Rückgabewert

Ein nicht freigegebenes FileStream-Objekt am angegebenen Pfad mit Write-Zugriff.

Ausnahmen

Ausnahmetyp Bedingung

UnauthorizedAccessException

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

- oder -

path gibt eine schreibgeschützte Datei oder ein schreibgeschütztes Verzeichnis an.

ArgumentException

path ist eine Zeichenfolge der Länge 0, besteht nur aus Leerraum oder enthält ein oder mehr durch InvalidPathChars definierte ungültige Zeichen.

ArgumentNullException

path ist NULL (Nothing in Visual Basic).

PathTooLongException

Der angegebene Pfad und/oder der Dateiname überschreiten die vom System vorgegebene Höchstlänge. Beispielsweise müssen Pfade auf Windows-Plattformen weniger als 248 Zeichen und Dateinamen weniger als 260 Zeichen haben.

DirectoryNotFoundException

Der angegebene Pfad ist ungültig (z. B. befindet er sich auf einem nicht zugeordneten Laufwerk).

FileNotFoundException

Die in path angegebene Datei wurde nicht gefunden.

NotSupportedException

Das Format von path ist ungültig.

Hinweise

Diese Methode entspricht der FileStream(String,FileMode,FileAccess,FileShare)-Konstruktorüberladung.

Mit dem path-Parameter dürfen relative oder absolute Pfadinformationen angegeben werden. Relative Pfadinformationen werden relativ zum aktuellen Arbeitsverzeichnis interpretiert. Informationen über das Abrufen des aktuellen Arbeitsverzeichnisses finden Sie unter GetCurrentDirectory.

Ein Beispiel für die Verwendung dieser Klasse finden Sie im Beispielabschnitt. In der folgenden Tabelle sind Beispiele für andere typische oder verwandte E/A-Aufgaben aufgeführt.

Aufgabe

Beispiel in diesem Thema

Erstellen einer Textdatei.

Gewusst wie: Schreiben von Text in eine Datei

In eine Textdatei schreiben.

Gewusst wie: Schreiben von Text in eine Datei

Aus einer Textdatei lesen.

Gewusst wie: Lesen aus einer Textdatei

Text an eine Datei anfügen.

Gewusst wie: Öffnen und Anfügen an eine Protokolldatei

File.AppendText

FileInfo.AppendText

Eine Datei umbenennen oder verschieben.

File.Move

FileInfo.MoveTo

Eine Datei kopieren.

File.Copy

FileInfo.CopyTo

Die Größe einer Datei abrufen.

FileInfo.Length

Die Attribute einer Datei abrufen.

File.GetAttributes

Die Attribute einer Datei festlegen.

File.SetAttributes

Bestimmen, ob eine Datei vorhanden ist.

File.Exists

Aus einer Binärdatei lesen.

Gewusst wie: Lesen und Schreiben einer neu erstellten Datendatei

In eine Binärdatei schreiben.

Gewusst wie: Lesen und Schreiben einer neu erstellten Datendatei

Erstellen eines Verzeichnisses.

CreateDirectory

Directory

Beispiel

Im folgenden Beispiel wird eine Datei zum Lesen und Schreiben geöffnet.

Imports System
Imports System.IO
Imports System.Text

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

        ' Delete the file if it exists.
        If File.Exists(path) = False Then
            ' Create the file.
            fs = File.Create(path)
            fs.Close()
        End If

        ' Open the stream and read it back.
        fs = 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)
        fs.Close()

        'Open the stream and read it back.
        fs = File.OpenRead(path)
        Dim b(1024) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(temp.GetString(b))
        Loop
        fs.Close()
    End Sub
End Class
using System;
using System.IO;
using System.Text;

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

        // Delete the file if it exists.
        if (!File.Exists(path)) 
        {
            // Create the file.
            using (FileStream fs = File.Create(path)) {}
        }

        // Open the stream and read it back.
        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));
            }
        }
    }
}
using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Delete the file if it exists.
   if (  !File::Exists( path ) )
   {
      // Create the file.
      FileStream^ fs = File::Create( path );
      if ( fs )
            delete (IDisposable^)fs;
   }

   // Open the stream and read it back.
   {
      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;
      }
   }
}
import System.*;
import System.IO.*;
import System.Text.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";

        // Delete the file if it exists.
        if (!(File.Exists(path))) {
            // Create the file.
            FileStream fs = File.Create(path);
            try {
            }
            finally {
                fs.Dispose();
            }
        }

        {
            // Open the stream and read it back.
            FileStream fs = File.OpenWrite(path);
            try {
                ubyte info[] = (new UTF8Encoding(true)).GetBytes("This is " 
                    + " to test the OpenWrite method.");

                // Add some information to the file.
                fs.Write(info, 0, info.length);
            }
            finally {
                fs.Dispose();
            }
        }

        // Open the stream and read it back.
        FileStream fs = File.OpenRead(path);
        try {
            ubyte b[] = new ubyte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b, 0, b.length) > 0) {
                Console.WriteLine(temp.GetString(b));
            }
        }
        finally {
            fs.Dispose();
        }
    } //main
} //Test

.NET Framework-Sicherheit

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

File-Klasse
File-Member
System.IO-Namespace

Weitere Ressourcen

Datei- und Stream-E/A
Gewusst wie: Lesen aus einer Textdatei
Gewusst wie: Schreiben von Text in eine Datei
Grundlegende Datei-E/A
Gewusst wie: Lesen und Schreiben einer neu erstellten Datendatei