Condividi tramite


FileInfo.CreateText Metodo

Definizione

Crea un oggetto StreamWriter che scrive un nuovo file di testo.

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

Restituisce

Nuovo oggetto StreamWriter.

Eccezioni

Il nome file è una directory.

Il disco è di sola lettura.

Il chiamante non dispone dell'autorizzazione richiesta.

Esempio

Nell'esempio seguente viene illustrato il CreateText metodo.

using namespace System;
using namespace System::IO;

int main()
{
   String^ path = "c:\\MyTest.txt";
   FileInfo^ fi = gcnew FileInfo( path );
   if (  !fi->Exists )
   {
      //Create a file to write to.
      StreamWriter^ sw = fi->CreateText();
      try
      {
         sw->WriteLine( "Hello" );
         sw->WriteLine( "And" );
         sw->WriteLine( "Welcome" );
      }
      finally
      {
         if ( sw )
            delete (IDisposable^)sw;
      }
   }

   //Open the file to read from.
   StreamReader^ sr = fi->OpenText();
   try
   {
      String^ s = "";
      while ( s = sr->ReadLine() )
      {
         Console::WriteLine( s );
      }
   }
   finally
   {
      if ( sr )
         delete (IDisposable^)sr;
   }
}
//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//Hello
//And
//Welcome
using System;
using System.IO;

class Test
{

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

        if (!fi.Exists)
        {
            //Create a file to write to.
            using (StreamWriter sw = fi.CreateText())
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
        }

        //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
Imports System.IO
Imports System.Text

Public Class Test

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

        If fi.Exists = False Then
            'Create a file to write to.
            Dim sw As StreamWriter = fi.CreateText()
            sw.WriteLine("Hello")
            sw.WriteLine("And")
            sw.WriteLine("Welcome")
            sw.Flush()
            sw.Close()
        End If

        'Open the file to read from.
        Dim sr As StreamReader = fi.OpenText()

        Do While sr.Peek() >= 0
            Console.WriteLine(sr.ReadLine())
        Loop
        sr.Close()
    End Sub
End Class
'This code produces output similar to the following; 
'results may vary based on the computer/file structure/etc.:
'
'Hello
'And
'Welcome

Commenti

Per impostazione predefinita, l'accesso in lettura/scrittura completo ai nuovi file viene concesso a tutti gli utenti.

Si applica a

Vedi anche