码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-02-17 12:44:21      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

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


那在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

 

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

标签:

原文地址:http://www.cnblogs.com/insus/p/5194839.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!