共用方式為


使用 My.Application、My.Computer 和 My.User 執行工作 (Visual Basic)

提供存取資訊與常用功能的三個中央 My 物件是 My.Application (ApplicationBase)、My.Computer (Computer) 與 My.User (User)。 您可以使用這些物件分別存取與目前應用程式相關的資訊、已安裝應用程式的電腦,或應用程式的目前使用者。

My.Application、My.Computer 與 My.User

下列範例示範如何使用 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*")

除了擷取資訊之外,透過這三個物件公開的成員也可讓您執行與該物件相關的方法。 例如,您可以存取各種方法來操作檔案,或透過 My.Computer 更新登錄。

使用 My 的檔案 I/O 明顯更容易且更快速,其中包含各種用於操作檔案、目錄與磁碟機的方法與屬性。 TextFieldParser 物件可讓您從具有分隔或固定寬度欄位的大型結構化檔案中讀取。 此範例會開啟 TextFieldParser reader 並將其用於從 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 可讓您變更應用程式的文化特性。 下列範例示範如何呼叫此方法。

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

另請參閱