Write to Text File

Posted on 2005-04-25
Last Modified: 2013-11-18
I have a simple FileSystemWatcher app (code below) - what I want to do is to write a new line to a text file on every event (file created, deleted, etc.)
Imports System.IO
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents objWatcher As New clsFSW()

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents cmdStart As System.Windows.Forms.Button
    Friend WithEvents cmdStop As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtFolder As System.Windows.Forms.TextBox
    Friend WithEvents chkSubFolders As System.Windows.Forms.CheckBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.cmdStart = New System.Windows.Forms.Button()
        Me.cmdStop = New System.Windows.Forms.Button()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtFolder = New System.Windows.Forms.TextBox()
        Me.chkSubFolders = New System.Windows.Forms.CheckBox()
        Me.TextBox1.Location = New System.Drawing.Point(32, 8)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox1.Size = New System.Drawing.Size(392, 240)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        Me.cmdStart.Location = New System.Drawing.Point(120, 336)
        Me.cmdStart.Name = "cmdStart"
        Me.cmdStart.TabIndex = 1
        Me.cmdStart.Text = "Start Watch"
        Me.cmdStop.Location = New System.Drawing.Point(216, 336)
        Me.cmdStop.Name = "cmdStop"
        Me.cmdStop.TabIndex = 2
        Me.cmdStop.Text = "Stop Watch"
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(6, 272)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(93, 13)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "Folder to Monitor:"
        Me.txtFolder.Location = New System.Drawing.Point(110, 272)
        Me.txtFolder.Name = "txtFolder"
        Me.txtFolder.Size = New System.Drawing.Size(184, 20)
        Me.txtFolder.TabIndex = 4
        Me.txtFolder.Text = ""
        Me.chkSubFolders.Location = New System.Drawing.Point(312, 272)
        Me.chkSubFolders.Name = "chkSubFolders"
        Me.chkSubFolders.Size = New System.Drawing.Size(128, 24)
        Me.chkSubFolders.TabIndex = 6
        Me.chkSubFolders.Text = "Include Subfolders"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 373)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkSubFolders, Me.txtFolder, Me.Label1, Me.cmdStop, Me.cmdStart, Me.TextBox1})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "File System Watcher"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmdStop.Enabled = False
        With objWatcher

        End With
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        Dim sDir As String
        sDir = txtFolder.Text

        If IO.Directory.Exists(sDir) Then
            objWatcher.FolderToMonitor = sDir
            cmdStop.Enabled = True
            cmdStart.Enabled = False
            chkSubFolders.Enabled = False
            MessageBox.Show("Folder does not exist!")

        End If
    End Sub

    Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
        cmdStop.Enabled = False
        cmdStart.Enabled = True
        chkSubFolders.Enabled = True

    End Sub

    Private Sub chkSubFolders_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSubFolders.CheckedChanged
        objWatcher.IncludeSubfolders = (chkSubFolders.Checked = True)
    End Sub

    Private Sub objWatcher_FileCreated(ByVal FullPath As String) Handles objWatcher.FileCreated
        TextBox1.Text &= "File Created: " & FullPath & vbCrLf
    End Sub

    Private Sub objWatcher_FileChanged(ByVal FullPath As String) Handles objWatcher.FileChanged
        TextBox1.Text &= "File Changed: " & FullPath & vbCrLf
    End Sub

    Private Sub objWatcher_FileDeleted(ByVal FullPath As String) Handles objWatcher.FileDeleted
        TextBox1.Text &= "File Deleted: " & FullPath & vbCrLf
    End Sub

    Private Sub objWatcher_FileRenamed(ByVal OldFileName As String, ByVal newFileName As String) Handles objWatcher.FileRenamed
        TextBox1.Text &= "File Rename: " & OldFileName & " to " & newFileName & vbCrLf
    End Sub

    Private Sub objWatcher_FileWatchError(ByVal ErrMsg As String) Handles objWatcher.FileWatchError
        TextBox1.Text &= "The following error occurred: " & ErrMsg & vbCrLf
    End Sub
End Class
Question by:tbaseflug
    1 Comment
    LVL 8

    Accepted Solution

    to write to a text file is like this:

    Imports System.IO
        Public Sub main()
           Dim f As File
            Dim filewrite As IO.StreamWriter
            'create file and write to it....
            filewrite = f.AppendText("c:\textfile.txt")
            'now write to the file
            filewrite.WriteLine("what u want to write")
    end sub

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now