VB编程基础学习系列—获取计算机名和系统目录
以前自己写的一套VB编程基础系列教程,适合VB初学者。
VB获取计算机名:
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Command1_Click()
Dim name As String, length As Long
length = 255
name = String(length, Chr(0)) '产生225个空字符,Chr(0)为空字符
GetComputerName name, length
name = Left(name, length) '这行代码可以不要
MsgBox name
End Sub
VB获取系统目录:
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Sub Command1_Click()
Dim mulu As String, length As Long
length = 200
mulu = String(length, Chr(0))
GetSystemDirectory mulu, length
mulu = Left(mulu, length)
MsgBox mulu, vbOKOnly + vbOKCancel, "系统提示"
End Sub
注意点:
String函数:返回 Variant (String),其中包含指定长度重复字符的字符串。