Windows ディレクトリのパスを取得する (GetWindowsDirectory)
kernel32.dll の GetWindowsDirectory を用いて、Windows ディレクトリのパスを取得します。
サンプルのダウンロード
■サンプルの説明

プログラムが開始すると、こんな画面が立ち上がります。

Button1 を押下するとこのようなメッセージが表示されます。
■コード
F0001_GetWindowsDirectory.vb
Imports System.Runtime.InteropServices ''' <summary> ''' GetWindowsDirectory(kernel32.dll) ''' </summary> Partial Public Class F0001_GetWindowsDirectory ''' <summary> ''' GetWindowsDirectoryの宣言 ''' </summary> <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function GetWindowsDirectory( _ ByVal lpBuffer As String, _ ByVal size As Integer) As Integer End Function ''' <summary> ''' バッファの最大サイズ ''' </summary> Private Const MAX_SIZE As Integer = 260 ''' <summary> ''' ロード時のイベント ''' </summary> Private Sub F0001_GetWindowsDirectory_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Me.Load Me.Text = "F0001_GetWindowsDirectory" End Sub ''' <summary> ''' Button1クリック時のイベント ''' </summary> Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Dim winDir As String = Space(MAX_SIZE) Dim res As Integer = GetWindowsDirectory(winDir, MAX_SIZE) winDir = winDir.Substring(0, res) MessageBox.Show("Windowsのディレクトリは" + winDir + "です", _ Me.Text, _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Sub End Class