Procedura: scrivere testo su un file

Aggiornamento: novembre 2007

Nell'esempio di codice riportato di seguito viene illustrata la procedura per la scrittura di testo in un file di testo.

Nel primo esempio viene mostrato come aggiungere testo a un file esistente. Nel secondo esempio viene mostrato come creare un nuovo file di testo e scrivere una stringa in tale file. Una funzionalità simile può essere fornita dai metodi WriteAllText.

Nota:

Gli utenti di Visual Basic possono scegliere di utilizzare metodi e proprietà forniti dall'oggetto My.Computer.FileSystem per l'I/O sul file. Per ulteriori informazioni, vedere Oggetto My.Computer.FileSystem.

Esempio

Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
        ' Create an instance of StreamWriter to write text to a file.
        Using sw As StreamWriter = New StreamWriter("TestFile.txt")
            ' Add some text to the file.
            sw.Write("This is the ")
            sw.WriteLine("header for the file.")
            sw.WriteLine("-------------------")
            ' Arbitrary objects can also be written to the file.
            sw.Write("The date is: ")
            sw.WriteLine(DateTime.Now)
            sw.Close()
        End Using
    End Sub
End Class
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Create an instance of StreamWriter to write text to a file.
        // The using statement also closes the StreamWriter.
        using (StreamWriter sw = new StreamWriter("TestFile.txt")) 
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}
Option Explicit On 
Option Strict On
Imports System
Imports System.IO
Public Class TextToFile
    Private Const FILE_NAME As String = "MyFile.txt"
    Public Shared Sub Main()
        If File.Exists(FILE_NAME) Then
            Console.WriteLine("{0} already exists.", FILE_NAME)
            Return
        End If
        Using sw As StreamWriter = File.CreateText(FILE_NAME)
            sw.WriteLine("This is my file.")
            sw.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)
            sw.Close()
        End Using
    End Sub
End Class
using System;
using System.IO;
public class TextToFile 
{
    private const string FILE_NAME = "MyFile.txt";
    public static void Main(String[] args) 
    {
        if (File.Exists(FILE_NAME)) 
        {
            Console.WriteLine("{0} already exists.", FILE_NAME);
            return;
        }
        using (StreamWriter sw = File.CreateText(FILE_NAME))
        {
            sw.WriteLine ("This is my file.");
            sw.WriteLine ("I can write ints {0} or floats {1}, and so on.", 
                1, 4.2);
            sw.Close();
        }
    }
}

Vedere anche

Attività

Procedura: creare una visualizzazione directory

Procedura: leggere e scrivere su un file di dati appena creato

Procedura: aprire e accodare un file di log

Procedura: leggere testo da un file

Procedura: leggere caratteri da una stringa

Procedura: scrivere caratteri in una stringa

Concetti

I/O di file di base

Riferimenti

StreamWriter

File.CreateText