Export (0) Print
Expand All

Performing Tasks with My.Application, My.Computer, and My.User (Visual Basic)

The three central My objects that provide access to information and commonly used functionality are My.Application (ApplicationBase), My.Computer (Computer), and My.User (User). You can use these objects to access information that is related to the current application, the computer that the application is installed on, or the current user of the application, respectively.

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 = 
  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")
Show:
© 2014 Microsoft