[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Write to Text File

Posted on 2005-04-25
Medium Priority
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

Accepted Solution

bman9111 earned 2000 total points
ID: 13861507
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

834 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