İngilizce dilinde oku

Aracılığıyla paylaş


FileInfo.AppendText Yöntem

Tanım

bu örneği FileInfotarafından temsil edilen dosyaya metin ekleyen bir StreamWriter oluşturur.

C#
public System.IO.StreamWriter AppendText ();

Döndürülenler

Yeni StreamWriterbir .

Örnekler

Aşağıdaki örnek bir dosyaya metin ekler ve dosyadan okur.

C#
using System;
using System.IO;

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

        // This text is added only once to the file.
        if (!fi.Exists)
        {
            //Create a file to write to.
            using (StreamWriter sw = fi.CreateText())
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }	
        }

        // This text will always be added, making the file longer over time
        // if it is not deleted.
        using (StreamWriter sw = fi.AppendText())
        {
            sw.WriteLine("This");
            sw.WriteLine("is Extra");
            sw.WriteLine("Text");
        }	

        //Open the file to read from.
        using (StreamReader sr = fi.OpenText())
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//
//Hello
//And
//Welcome
//This
//is Extra
//Text

//When you run this application a second time, you will see the following output:
//
//Hello
//And
//Welcome
//This
//is Extra
//Text
//This
//is Extra
//Text

Aşağıdaki örnek, dosyanın sonuna metin eklemeyi gösterir ve ayrıca konsola ekleme işleminin sonucunu görüntüler. Bu yordam ilk kez çağrıldığında, dosya yoksa oluşturulur. Bundan sonra, belirtilen metin dosyaya eklenir.

C#
using System;
using System.IO;

public class AppendTextTest
{
    public static void Main()
    {
        FileInfo fi = new FileInfo("temp.txt");
        // Create a writer, ready to add entries to the file.
        StreamWriter sw = fi.AppendText();
        sw.WriteLine("Add as many lines as you like...");
        sw.WriteLine("Add another line to the output...");
        sw.Flush();
        sw.Close();
        // Get the information out of the file and display it.
        // Remember that the file might have other lines if it already existed.
        StreamReader sr = new StreamReader(fi.OpenRead());
        while (sr.Peek() != -1)
            Console.WriteLine( sr.ReadLine() );
    }
}
//This code produces output similar to the following;
//results may vary based on the computer/file structure/etc.:
//Add as many lines as you like...
//Add another line to the output...

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Ayrıca bkz.