您的位置:程序门 -> .net技术 -> c#



利用wmi,如何高效获得系统进程的用户名?高分求教!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


利用wmi,如何高效获得系统进程的用户名?高分求教![已结贴,结贴人:pk_ricky]
发表于:2007-03-28 19:43:32 楼主
我用这种方法得到相应进程的相关信息,其中用invokemethod()的方法得到进程用户名,但是效率相对太低(要多执行近1秒),希望高手提供一个效率高的方法得到这个项,最好用wql直接查询。
managementobjectcollection   processbase   =   wmiquery( "* ",   "win32_process ");                      
foreach(managementobject   obj   in   processbase)
{
          console.write(obj.properties[ "processid "].value.tostring());
          console.write(obj.properties[ "name "].value.tostring());
          if   (obj.properties[ "processid "].value.tostring()   !=   "0 ")
          {
                  object   user   =   obj.invokemethod( "getowner ",   null,   new   invokemethodoptions(null,   timespan.maxvalue));
                  console.write(((managementbaseobject)user).properties[ "user "].value.tostring());
          }
          else
                  console.write( "system ");
          console.write(buffertable[obj.properties[ "processid "].value.tostring()].tostring());
          console.writeline(obj.properties[ "workingsetsize "].value.tostring());
}

其中:protected   managementobjectcollection   wmiquery(string   sltitems,   string   slttarget)
{
          managementobjectsearcher   msearcher   =   new   managementobjectsearcher();
          msearcher.scope   =   new   managementscope(@ "\\localhost\root\cimv2 ");
          msearcher.query   =   new   objectquery(string.format( "select   {0}   from   {1} ",   sltitems,   slttarget));
          return   msearcher.get();
}
发表于:2007-03-28 19:44:241楼 得分:0
自己先顶!
发表于:2007-03-28 20:02:582楼 得分:0
非要wmi么,为什么不用processinfo呢?
发表于:2007-03-28 23:16:333楼 得分:0
up
发表于:2007-03-29 09:27:474楼 得分:0
up
发表于:2007-04-04 15:51:405楼 得分:40
http://www.codeproject.com/cs/system/wmi.asp


快速检索

最新资讯
热门点击