Dil

FileInfo.OpenWrite Yöntem

Tanım

Salt FileStreamyazma oluşturur.

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

Döndürülenler

Yeni veya var olan bir dosya için salt yazma özellikli paylaşılmayan FileStream nesne.

Özel durumlar

Nesnenin bir örneğini FileInfo oluştururken belirtilen yol salt okunurdur veya bir dizindir.

Nesnenin bir örneğini FileInfo oluştururken belirtilen yol, eşlenmemiş bir sürücüde olmak gibi geçersiz.

Örnekler

Aşağıdaki örnek, yazmak için bir dosya açar ve dosyadan okur.

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;
//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.
'
'
'
'
'
'
'
'
'
'
'
'

Açıklamalar

yöntemi, OpenWrite dosya yolu için zaten bir dosya varsa dosyayı açar veya yoksa yeni bir dosya oluşturur. Varolan bir dosya için, yeni metni var olan metne eklemez. Bunun yerine, var olan karakterlerin üzerine yeni karakterler yazar. Daha kısa bir dizeyle ("Bu, OpenWrite yönteminin bir testidir") daha uzun bir dizenin üzerine yazarsanız ("İkinci çalıştırma" gibi), dosya dizelerin bir karışımını ("OpenWrite yönteminin ikinci çalıştırma testi")içerir.

Şunlara uygulanır