一個程式也許會被多個用戶運行,如下:那在VB.NET的WinForm環境下,怎樣獲取User Name呢?可從下麵的方法: 代碼: Public Shared Function GetProcessOwner(ByVal ProcessName As String) As String Dim po
一個程式也許會被多個用戶運行,如下:
那在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 FunctionView Code