Esecuzione di attività mediante My.Application, My.Computer e My.User (Visual Basic)

I tre oggetti centrali My che forniscono l'accesso alle informazioni e le funzionalità comunemente usate sono My.Application (ApplicationBase), My.Computer (Computer) e My.User (User). È possibile usare questi oggetti per accedere rispettivamente alle informazioni correlate all'applicazione corrente, al computer in cui è installata l'applicazione o all'utente corrente dell'applicazione.

My.Application, My.Computer e My.User

Negli esempi seguenti viene illustrato come recuperare le informazioni usando 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*")

Oltre a recuperare informazioni, i membri esposti tramite questi tre oggetti consentono anche di eseguire metodi correlati a tale oggetto. Ad esempio, è possibile accedere a un'ampia gamma di metodi per modificare i file o aggiornare il Registro di sistema tramite My.Computer.

L'I/O dei file è notevolmente più semplice e veloce con My, che include un'ampia gamma di metodi e proprietà per la modifica di file, directory e unità. L'oggetto TextFieldParser consente di leggere da file strutturati di grandi dimensioni con campi delimitati o a larghezza fissa. In questo esempio viene aperto TextFieldParserreader e viene usato per leggere da 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 consente di modificare le impostazioni cultura per l'applicazione. Nel seguente esempio viene illustrato come chiamare questo metodo.

' Changes the current culture for the application to Jamaican English.
My.Application.ChangeCulture("en-JM")

Vedi anche