Code Snippets

7 views
Skip to first unread message

Armyman

unread,
Apr 15, 2010, 4:47:08 PM4/15/10
to Visual Basic Express Users
Well to get things started, I'll share a few of my favorite code
snippets that I find useful in some of the code I use everyday:

(Logfile) - In most every program I have written lately, i've had a
need to keep a log file of user activities. This was a challenge for
me at first, but the thing that makes this code a LOT easier on you is
if you understand the "Settings" feature. In the code below, I've
setup a my.settings string named "logloc" (Log File Location) that
points to the directory where my logfile exist (C:\program\log.txt)
then the code creates a virtual textbox, reads the logfile, then
appends the (VT) or whatever I wish to add to the log at the TOP of
the logfile, then creates a new line, so that when I view the log
file, the newest entry is at the top.

If you need more help with this code, let me know and I'll be happy to
help you configure it to your needs.

-------------------------

Dim myfile As String = My.Settings.logloc
Dim itxt As New TextBox
If IO.File.Exists(myfile) Then
itxt.Text = IO.File.ReadAllText(myfile)
End If
Dim vt As String = vbCrLf & Date.Now & " - " &
My.Application.Info.Version.ToString & ex.ToString & My.User.Name &
itxt.Text
My.Computer.FileSystem.WriteAllText(myfile, vt, False)
itxt.Clear()


--------------------------

Armyman

unread,
Apr 21, 2010, 4:04:24 PM4/21/10
to Visual Basic Express Users
EDIT:

the line of code that reads: My.Application.Info.Version.ToString &
ex.ToString & My.User.Name & itxt.Text
is incorrect and should read: My.Application.Info.Version.ToString &
"BLAH BLAH" & My.User.Name & itxt.Text

the "ex.tostring" is only if you put that in a TRY....Catch ,
otherwise you can put anything you like between the & signs
--
You received this message because you are subscribed to the Google Groups "Visual Basic Express Users" group.
To post to this group, send email to visual-basic-...@googlegroups.com.
To unsubscribe from this group, send email to visual-basic-expres...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/visual-basic-express-users?hl=en.

Reply all
Reply to author
Forward
0 new messages