ヘッダー

VB.NET 2002 対応 VB.NET 2003 対応 VB2005 対応

 

Windowsフォルダの場所を取得する

VB.NET 2002 対応 VB.NET 2003 対応 VB2005 対応


WindowsFolder = Environment.GetEnvironmentVariable("WINDIR")
 

メモ:Windowsフォルダは標準ではWindows95/98/Me/XPは「C:\Windows」、Windows NT/2000では「C:\Winnt」。しかし、標準でインストールしない人やドライブを変えている人がいるので上記の例のようにWindowsフォルダの場所を取得する必要がある。

 

参考:環境変数に依存しない方法

VB.NET 2002 対応 VB.NET 2003 対応 VB2005 対応

Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Integer) As Integer
Private Function WindowsFolder() As String

  Dim FolderName As String = New String(" ", 260)
  Dim RetLength As Integer

  RetLength = GetWindowsDirectory(FolderName, FolderName.Length)

  Return FolderName.Substring(0, RetLength)

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  MsgBox(WindowsFolder())

End Sub

 

参照

システムフォルダの場所を取得する Tempフォルダの場所を取得する


VB6対応 VB6では「Windowsフォルダの場所を取得する」を参照。