我需要当前用户和域名。我正在使用VB 6应用程序。
由于
答案 0 :(得分:15)
一种方法是询问环境:
Dim UserName As String
Dim UserDomain As String
UserName = Environ("USERNAME")
UserDomain = Environ("USERDOMAIN")
(显然适用于Windows NT及以上版本。)
答案 1 :(得分:8)
API版本:
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long
Private Sub Form_Load()
Dim sDomainName As String * 255
Dim lDomainNameLength As Long
Dim sUserName as String
Dim bUserSid(255) As Byte
Dim lSIDType As Long
Rem Create a buffer
sUserName = String(100, Chr$(0))
Rem Get the username
GetUserName sUserName, 100
Rem strip the rest of the buffer
sUserName = Left$(sUserName, InStr(sUserName, Chr$(0)) - 1)
rem Show the temppath and the username
MsgBox "Hello " + strUserName
lResult = LookupAccountName(vbNullString, sUserName, bUserSid(0), 255, sDomainName, lDomainNameLength, _
lSIDType)
if lResult <>0 then
msgbox sDomainName
end if
end sub
答案 2 :(得分:4)
使用WshNetwork对象的以下方法,在您的项目中引用Windows脚本宿主对象模型后可以使用该方法:
Dim Network As WshNetwork
Set Network = New WshNetwork
Debug.Print "ComputerName: " & Network.ComputerName
Debug.Print "UserDomain: " & Network.UserDomain
Debug.Print "UserName: " & Network.UserName
我将结果转换为大写或小写以保持一致性,但这些是您需要的方法。
请注意,在未登录到域的计算机上运行时,ComputerName和UserDomain都会返回相同的内容 - 计算机名称。
答案 3 :(得分:2)
基本上您需要进行Windows API调用。 搜索vbnet.mvps.org我得到以下答案。
答案 4 :(得分:0)
这个怎么样?
Private Function IsAdmin() As Boolean
Dim groups As Object
Dim user As Object
Set groups = GetObject("WinNT://./administrators")
For Each user In groups.members
If UCase(Environ("USERNAME")) = UCase(user.Name) Then
IsAdmin = True
End If
Next user
End Function