File.OpenWrite(String) Metodo

Definizione

Apre un file esistente o crea un nuovo file per la scrittura.

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

Parametri

path
String

File da aprire per la scrittura.

Restituisce

FileStream

Oggetto FileStream non condiviso nel percorso specificato con accesso a Write.

Eccezioni

Il chiamante non dispone dell'autorizzazione richiesta.

-oppure- path ha specificato un file o una directory di sola lettura.

.NET Framework e .NET Core precedenti alla 2.1: è una stringa di lunghezza zero, contiene solo spazi vuoti o contiene uno o più path caratteri non validi. È possibile cercare i caratteri non validi usando il metodo GetInvalidPathChars().

path è null.

Il percorso specificato, il nome file o entrambi superano la lunghezza massima definita dal sistema.

Il percorso specificato non è valido, ad esempio si trova in un'unità non mappata.

Il formato di path non è valido.

Esempio

Nell'esempio seguente viene aperto un file per la lettura e la scrittura.

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));
            }
        }
    }
}
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

Commenti

Questo metodo equivale all'overload del costruttore con la modalità file impostata su , l'accesso FileStream(String, FileMode, FileAccess, FileShare) impostato su e la modalità di condivisione OpenOrCreate Write impostata su None .

Il metodo apre un file se ne esiste già uno per il percorso del file oppure crea OpenWrite un nuovo file se non ne esiste uno. Per un file esistente, non aggiunge il nuovo testo al testo esistente. Sovrascrive invece i caratteri esistenti con i nuovi caratteri. Se si sovrascrive una stringa più lunga ,ad esempio "This is a test of the OpenWrite method" (Test del metodo OpenWrite) con una stringa più breve ,ad esempio "Second run", il file conterrà una combinazione di stringhe ("Second runtest of the OpenWrite method").

Il path parametro può specificare informazioni relative o assolute sul percorso. Le informazioni sul percorso relativo vengono interpretate come relative alla directory di lavoro corrente. Per ottenere la directory di lavoro corrente, usare il GetCurrentDirectory metodo .

L'oggetto FileStream restituito non supporta la lettura. Per aprire un file sia per la lettura che per la scrittura, usare Open .

Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Si applica a

Vedi anche