C # Treegridview implementation process List

Source: Internet
Author: User

Effect

0x01 gets the list of processes and uses WIN32API to circumvent "Access denied" exceptions

 PublicList<appprocess>getappprocesses () {INTPTR handle= Nativemethods.createtoolhelp32snapshot (0x2,0); List<ProcessEntry32> list =NewList<processentry32>(); List<AppProcess> applist =NewList<appprocess>(); if((int) Handle >0) {ProcessEntry32 pe32=NewProcessEntry32 (); Pe32.dwsize= (UINT) marshal.sizeof (PE32); intBmore = Nativemethods.process32first (handle,refpe32);  while(Bmore = =1) {ProcessEntry32 PE=pe32.                    Marshalex (); //rule out [System Process]                    if(Pe.th32processid >0) {IntPtr ProcessHandle= Nativemethods.openprocess (nativemethods.process_all_access,true, Pe.th32processid); //Exclude inaccessible                        if(ProcessHandle! =IntPtr.Zero) {Pe.processhandle=ProcessHandle; List.                        ADD (PE); }                        Else                        {                            varErr =Marshal.GetLastWin32Error (); Applist. ADD (Newappprocess {Process id = pe.th32processid, file name = Pe.szexefile, parent process id =Pe.th32parentprocessid}); }} bmore= Nativemethods.process32next (Handle,refpe32);            }} nativemethods.closehandle (handle); foreach(ProcessEntry32 Pinchlist) {                varProcessHandle =P.processhandle; varWinexepath =NewStringBuilder ( +); varLen = Nativemethods.getmodulefilenameex (ProcessHandle, IntPtr.Zero, Winexepath, (UINT) winexepath.capacity); if(Len >0)                {                    varPath =winexepath.tostring (); varBaseName =P.szexefile; varDescription =""; varManifuture =""; Try                    {                        varErr =0; varBasenamesb =NewStringBuilder ( -); varNamelen = Nativemethods.getmodulebasename (NewSafeprocesshandle (ProcessHandle,false),0, Basenamesb, basenamesb.capacity); if(Namelen >0) {BaseName=basenamesb.tostring (); }                        Else{Err=Marshal.GetLastWin32Error (); } process_basic_information PBI=Newprocess_basic_information (); intsizeinforeturned; intQueryStatus = Nativemethods.ntqueryinformationprocess (ProcessHandle, (Processinfoclass)0,refPBI, Marshal.SizeOf (PBI), outsizeinforeturned);                        Nativemethods.closehandle (ProcessHandle); varPEB =PBI.                        pebbaseaddress; FileVersionInfo Info=fileversioninfo.getversioninfo (path); Description=info.                        FileDescription; Manifuture=Info.companyname; }                    Catch(FileNotFoundException) {}Catch(Exception ex) {} applist. ADD (Newappprocess {manufacturer = manifuture, Process id = p.th32processid, file name = BaseName, self-description = description, File path = path, parent process id = P.th32par Entprocessid >0? P.th32parentprocessid: (UINT?)NULL }); }                Else                {                    varErr =Marshal.GetLastWin32Error (); Console.WriteLine ("Process"+ P +"failed to get module path. Error code"+err); }            }            returnapplist; }

0x02 recursive list to tree structure

Private void Setsubitems (ienumerable<appprocess> rootlist, ienumerable<appprocess> plist)        {              foreach (var in rootlist)            {                foreach (varin  plist)                {                    if (item. parent Process id = = rootitem. Process id)                    {                        ROOTITEM.SUBITEMS.ADD (item);                    }                }                Setsubitems (Rootitem.subitems, plist);            }        }

0x03 recursive tree structure bound to a control node

Private void Setnodes (ienumerable<appprocess> rootlist, treegridnodecollection nodes)        {            foreach (var in rootlist)            {                var node = nodes. ADD (item. filename, item. Process ID, item. file path, item. Manufacturer, item. description);                 0 ;                Setnodes (item. SubItems, node. Nodes);            }        }

Group sharing to get the source code. NET Software Team 283590657

C # Treegridview implementation process List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.