FileInfo.OpenWrite Méthode

Définition

Crée un élément FileStream en écriture seule.

public:
 System::IO::FileStream ^ OpenWrite();
public System.IO.FileStream OpenWrite ();
member this.OpenWrite : unit -> System.IO.FileStream
Public Function OpenWrite () As FileStream

Retours

Objet FileStream en écriture seule non partagé pour un fichier nouveau ou existant.

Exceptions

Le chemin spécifié pendant la création d’une instance de l’objet FileInfo est en lecture seule ou correspond à un répertoire.

Le chemin spécifié pendant la création d’une instance de l’objet FileInfo n’est pas valide . il est, par exemple, sur un lecteur non mappé.

Exemples

L’exemple suivant ouvre un fichier pour l’écriture, puis lit à partir du fichier.

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

int main()
{
   String^ path = "c:\\Temp\\MyTest.txt";
   FileInfo^ fi = gcnew FileInfo( path );

   // Open the stream for writing.
   {
      FileStream^ fs = fi->OpenWrite();
      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 = fi->OpenRead();
      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;
      }
   }
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//This is to test the OpenWrite method.
//
//
//
//
//
//
//
//
//
//
//
//
using System;
using System.IO;
using System.Text;

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

        // Open the stream for writing.
        using (FileStream fs = fi.OpenWrite())
        {
            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 = fi.OpenRead())
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//This is to test the OpenWrite method.
//
//
//
//
//
//
//
//
//
//
//
Imports System.IO
Imports System.Text

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

        ' Open the stream for writing.
        fs = fi.OpenWrite()
        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 = fi.OpenRead()
        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
        fs.Close()
    End Sub
End Class
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'This is to test the OpenWrite method.
'
'
'
'
'
'
'
'
'
'
'
'

Remarques

La OpenWrite méthode ouvre un fichier s’il existe déjà pour le chemin d’accès au fichier, ou crée un fichier s’il n’en existe pas un. Pour un fichier existant, il n’ajoute pas le nouveau texte au texte existant. Au lieu de cela, il remplace les caractères existants par les nouveaux caractères. Si vous remplacez une chaîne plus longue (telle que « Il s’agit d’un test de la méthode OpenWrite ») par une chaîne plus courte (comme « Deuxième exécution »), le fichier contient une combinaison des chaînes (« Deuxième runtest de la méthode OpenWrite »).

S’applique à