取得Windows路徑

使用API

API名稱:GetWindowsDirectory
動態連結程式庫: kernel32.dll

VB6入口點:GetWindowsDirectoryA
VB.NET入口點: GetWindowsDirectoryW


程式說明

取得Windows資料夾之位置。


VB6程式碼

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

Private Sub Command1_Click()
    Dim buffer As String, length As Integer
    buffer = Space$(512)
    length = GetWindowsDirectory(buffer, Len(buffer))
    Text1.Text = "Windows Directory = " & Left$(buffer, length)
End Sub

VB.NET程式碼

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1
    <DllImport("kernel32.dll", SetLastError:=True, EntryPoint:="GetWindowsDirectoryW", CharSet:=CharSet.Unicode)>
    Shared Function GetWindowsDirectory(<MarshalAs(UnmanagedType.LPTStr)> lpBuffer As System.Text.StringBuilder,
                   uSize As UInteger) As UInteger
    End Function
    Shared Function wins()
        Dim sb As StringBuilder = New StringBuilder(100)
        Dim f As UInteger = 100
        GetWindowsDirectory(sb, f)
        Return sb.ToString()
    End Function
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = wins()
    End Sub
End Class

下載