取得系統路徑

使用API

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

VB6入口點:GetSystemDirectoryA
VB.NET入口點: GetSystemDirectoryW


程式說明

取得System32資料夾之位置。


VB6程式碼

Private Declare Function GetSystemDirectory Lib "kernel32" Alias _
    "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long
Dim buffer As String, length As Integer

Private Sub Command1_Click()
    buffer = Space$(512)
    length = GetSystemDirectory(buffer, Len(buffer))
    Text1.Text = "System Directory =" & Left$(buffer, length)
End Sub

VB.NET程式碼

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1
    <DllImport("kernel32.dll", SetLastError:=True, EntryPoint:="GetSystemDirectoryW", CharSet:=CharSet.Unicode)>
    Shared Function GetSystemDirectory(<MarshalAs(UnmanagedType.LPTStr)> lpBuffer As System.Text.StringBuilder,
                   uSize As UInteger) As UInteger
    End Function

    Shared Function wins() As String
        Dim sb As StringBuilder = New StringBuilder(100)
        Dim f As UInteger = 100
        GetSystemDirectory(sb, f)
        Return sb.ToString()
    End Function
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text = wins()
    End Sub
End Class

下載