获得系统文件夹

博客展示了通过调用 Windows API 函数获取系统相关文件夹路径的代码。包括使用 GetWindowsDirectory 函数获取 Windows 目录,GetTempPath 函数获取系统临时文件夹,GetSystemDirectory 函数获取系统文件夹,并对返回值进行处理和输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Option Explicit

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

Private Sub Command1_Click()
    '变量定义。
    Dim dstr1 As String
    dstr1 = String(255, 0)
    '调用函数。
    Call GetWindowsDirectory(dstr1, 255)
   
'返回数值处理。
   dstr1 = Left(dstr1, InStr(1, dstr1, Chr(0)) - 1)
  
'数据的输出。
   Text1.Text = dstr1
End Sub

Private Sub Command2_Click()
    End  '退出。
End Sub

'获得系统的临时文件夹
Option Explicit
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const Max_PATH = 260

Private Function GetTempDir() As String
  Dim TempDir As String
  Dim ChrLen As Long
  TempDir = Space(Max_PATH)
  ChrLen = GetTempPath(Max_PATH, TempDir)
  If ChrLen > Max_PATH Then
    ChrLen = GetTempPath(TempDir, ChrLen)
  End If
  GetTempDir = Left(TempDir, ChrLen)
End Function

Private Sub Form_Load()
  With Text1
    .Font = 18
    .Text = GetTempDir
  End With
End Sub

'获得系统文件夹
Option Explicit

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

Private Const MAX_PATH = 260

Private Function GetSysDir() As String
  Dim SysDir As String
  Dim ChrLen As String
  SysDir = Space(MAX_PATH)
  ChrLen = GetSystemDirectory(SysDir, MAX_PATH)
  If ChrLen > MAX_PATH Then
    ChrLen = GetSystemDirectory(SysDir, ChrLen)
  End If
  GetSysDir = Left(SysDir, ChrLen)
End Function

Private Sub Form_Load()
  Dim SysDir As String
  SysDir = GetSysDir
  Text1 = SysDir
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值