zoukankan      html  css  js  c++  java
  • Windows Service得到当前用户的名字和域

  • Create a Windows Service project using VB.NET.
  • Open Server Explorer and expand the tree node which says Management Classes.
  • Right click on Processes node and select "Generate Managed Class". This will add a reference to the System.Management namespace.
  • Now import System.Management namespace in your project (Service1.vb).
  • In the OnStart procedure, add the following code: 
  • Dim mc As New ManagementClass("Win32_Process")
    Dim moc As ManagementObjectCollection = mc.GetInstances
    Dim mo As ManagementObject
    Dim processDomain, processUser As String
    
     For Each mo In moc
    
         Dim p As New ROOT.CIMV2.Process(mo)
         p.GetOwner(processDomain, processUser)
    
           If (p.Name.Trim = "explorer.exe") Then
              Return processUser
              Exit For
           End If
      
     Next

    C#   using System.Management;
    string domainName = string.Empty;
                        string curUserName = string.Empty;
                        string nameCurrent = string.Empty;
                        try
                        {
                            ManagementClass ms = new ManagementClass("Win32_Process");
                            ManagementObjectCollection moc = ms.GetInstances();
                            
    
                            foreach( ManagementObject mo in moc)
                            {
                                ROOT.CIMV2.Process p = new Customization.Service.ROOT.CIMV2.Process(mo);
                                p.GetOwner(out domainName,out curUserName);
                                if(p.Name.Trim() == "explorer.exe")
                                {
                                    nameCurrent = domainName+"\\"+curUserName;
                                    break;
                                }
                            } 
                        }
                        catch
                        {
                            //do nothing
                        }
    

查看全文
  • 相关阅读:
    java学习笔记3
    java学习笔记 2
    linux用户登录指定目录
    [Jenkins 新插件] 兼容阿里开发手册 (P3C) 的火线插件安装使用教程
    Color a Tree HDU
    网络流初步
    K度限制MST poj 1639
    曼哈顿距离MST
    ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
    HDU Distinct Values
  • 原文地址:https://www.cnblogs.com/blackbean/p/2014557.html
  • Copyright © 2011-2022 走看看