File.CreateText : File « System.IO « VB.Net by API






File.CreateText

  

Imports System
Imports System.IO

public class LogTest
   public Shared Sub Main
        MyLog.Log("Started Up", "C:\test.txt")
        MyLog.Log(String.Format("DisplayName called, {0} {1} output","B", "A"), "C:\test.txt")
   End Sub
End class

Public Class MyLog
    Const DefaultLogFile As String = "C:\LogFile.txt"

    Shared Sub Log(ByVal Message As String,ByVal FileName As String)
        Dim objLogFile As StreamWriter
        
        If IO.File.Exists(FileName) Then
            objLogFile = File.AppendText(FileName)
        ElseIf Directory.Exists (Path.GetDirectoryName(FileName)) Then
            objLogFile = File.CreateText(FileName)
        Else
            objLogFile = File.AppendText(DefaultLogFile)
        End If

        objLogFile.WriteLine(Message)
        objLogFile.Flush()
        objLogFile.Close()
    End Sub
End Class

   
    
  








Related examples in the same category

1.File.AppendText
2.File.Copy
3.File.Delete
4.File.Exists
5.File.GetCreationTime
6.File.GetLastAccessTime
7.File.GetLastWriteTime
8.File.Move
9.File.OpenRead