
My.Application, My.Computer, and My.User
The following examples demonstrate how information can be retrieved using My.
' Displays a message box that shows the full command line for the
' application.
Dim args As String = ""
For Each arg As String In My.Application.CommandLineArgs
args &= arg & " "
Next
MsgBox(args)
' Gets a list of subfolders in a folder
My.Computer.FileSystem.GetDirectories _
(My.Computer.FileSystem.SpecialDirectories.MyDocuments, True, "*Logs*")
In addition to retrieving information, the members exposed through these three objects also allow you to execute methods related to that object. For instance, you can access a variety of methods to manipulate files or update the registry through My.Computer.
File I/O is significantly easier and faster with My, which includes a variety of methods and properties for manipulating files, directories, and drives. The TextFieldParser Object allows you to read from large structured files that have delimited or fixed-width fields. This example opens the TextFieldParser reader and uses it to read from C:\TestFolder1\test1.txt.
Dim reader As Microsoft.VisualBasic.FileIO.TextFieldParser
reader = My.Computer.FileSystem.OpenTextFieldParser _
("C:\TestFolder1\test1.txt")
reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
reader.Delimiters = New String() {","}
Dim currentRow As String()
While Not reader.EndOfData
Try
currentRow = reader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
MsgBox(currentField)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & _
"is not valid and will be skipped.")
End Try
End While
My.Application allows you to change the culture for your application. The following example demonstrates how this method can be called.
' Changes the current culture for the application to Jamaican English.
My.Application.ChangeCulture("en-JM")