My.Application、My.Computer、および My.User でのタスクの実行 (Visual Basic)

情報へのアクセスとよく使用される機能が提供される中心的な 3 つの 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*")

情報を取得するだけでなく、これら 3 つのオブジェクトを使用して公開されるメンバーは、そのオブジェクトに関連するメソッドを実行することもできます。 たとえば、My.Computer を使用して、さまざまなメソッドにアクセスして、ファイルの操作やレジストリの更新ができます。

ファイル、ディレクトリ、およびドライブを操作するためのさまざまなメソッドとプロパティが含まれているため、My を使用するとファイル I/O が大幅に簡単で高速になります。 TextFieldParser オブジェクトを使用すると、区切り記号または固定幅フィールドが含まれる大規模な構造化ファイルから読み取ることができます。 この例では、TextFieldParserreader を開き、それを使用して 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")

関連項目