VB.NET WinForm获取运行程序用户名

一个程序也许会被多个用户运行,如下:



那在VB.NET的WinForm环境下,怎样获取User Name呢?
可从下面的方法:


代码:

Public Shared Function GetProcessOwner(ByVal ProcessName As String) As String
            Dim po = String.Empty
            Dim selectQuery As SelectQuery = New SelectQuery("Win32_Process")
            Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(selectQuery)
            Dim moc As ManagementObjectCollection = searcher.Get()
            For Each proc As ManagementObject In moc
                Dim s(1) As String
                proc.InvokeMethod("GetOwner", CType(s, Object()))
                If proc("Name").ToString() = ProcessName & ".exe" Then
                    po = s(1) & "\\" & s(0)
                End If
            Next
            Return po
        End Function
View Code

 

posted @ 2016-02-17 11:45  Insus.NET  阅读(1387)  评论(0编辑  收藏  举报