流程列表及其详细信息

时间:2013-04-13 13:04:44

标签: c# process

对不起id我的qustion! 我编写了一个代码,在一个列表中显示进程,并且它是onther并且显示madual 我在c#2010阅读frist并且工作但是我需要它在c#2008中,在c#2008中我得到了 错误“访问Denaid”并且仅显示45个过程中的~5个:(

 private string GetProcessOwner(int ProcessID)
    {
        string ProcessOwner = "";
            ObjectQuery ObjQuery = new ObjectQuery("Select * From Win32_Process where ProcessID=" + ProcessID.ToString());
            ManagementObjectSearcher MgtObjSrch = new ManagementObjectSearcher(ObjQuery);
        foreach (ManagementObject MgtObj in MgtObjSrch.Get())
            {
                string[] s = new string[1];
                MgtObj.InvokeMethod("GetOwner", (object[])s);
                if (s[0] != null)
                ProcessOwner = s[0];
                break;
            }
                return ProcessOwner;
    }

    public void getprocesses()//start function ..........
    {
        ListViewItem lstitm;
        lsvproccess.Items.Clear();
        int index = 1;
        //get total ram
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
        foreach (ManagementObject share in searcher.Get())
        {
             RAMCapacityByte = Convert.ToInt32(share["TotalPhysicalMemory"]);
             RAMCapacityKB = RAMCapacityByte / 1024;
             RAMCapacityMB = RAMCapacityKB / 1024;
             RAMCapacityGB = RAMCapacityMB / 1024;
        }
        toolStripStatusLabel7.Text = RAMCapacityMB.ToString() + " Kb";// view total ram memory
        toolStripStatusLabel5.Text = Environment.UserName;//view user name login
        Int32 totalmemo = RAMCapacityKB;
        processes = Process.GetProcesses(); //trying to get local active processes 
        try
        {
            foreach (Process pro in processes)
            {
                if (pro.ProcessName != "Idle")
                {
                    var wallTime = DateTime.Now - pro.StartTime;
                    if (pro.HasExited)
                        wallTime = pro.ExitTime - pro.StartTime;
                    var procTime = pro.TotalProcessorTime;
                    var cpuUsage = 100 * (procTime.TotalMilliseconds / wallTime.TotalMilliseconds);
                    var memousage = (pro.PrivateMemorySize64 / 1024);
                    if (Environment.UserName == GetProcessOwner(pro.Id) && wallTime.TotalMinutes > 60)
                    {
                        DialogResult result;
                        result = MessageBox.Show("Prossecc: " + pro.ProcessName + "\n" + "Total Time: " + wallTime.TotalMinutes.ToString("00000") + " Min\n",
                            "Are you will close it?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.Yes)
                            pro.Kill();
                    }
                     if (cpuUsage > 1)
                    {
                        DialogResult result;
                        result = MessageBox.Show("Prossecc: " + pro.ProcessName + "\n" + "Cpu Usage: " + cpuUsage.ToString("0.000") + " Ms\n",
                            "Are you will close it?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                       if (result == DialogResult.Yes)
                            pro.Kill();
                    }
                     if (memousage > (totalmemo * 0.3))
                    {
                        DialogResult result;
                        result = MessageBox.Show("Prossecc: " + pro.ProcessName + "\n" + "Memo Usage: " + memousage.ToString() + " Kb",
                            "Are you will close it?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.Yes)
                            pro.Kill();
                    }
                     if (Environment.UserName == GetProcessOwner(pro.Id) && pro.MainModule.FileVersionInfo.CompanyName == "Microsoft corporation")
                    {
                        pro.Kill();
                        MessageBox.Show("Prossecc: " + pro.ProcessName + "\n",
                           "Is Terminat!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                    else
                    {
                        lstitm = new ListViewItem();
                        // binding listview instance to desired attributes
                        lstitm = lsvproccess.Items.Add(index.ToString("000"));
                        lstitm.SubItems.Add(pro.ProcessName);
                        lstitm.SubItems.Add(pro.Id.ToString());
                        lstitm.SubItems.Add(wallTime.TotalMinutes.ToString("000000") + " M");
                        lstitm.SubItems.Add(cpuUsage.ToString("0.000") + " ms");
                        lstitm.SubItems.Add(memousage.ToString() + " kb");
                        lstitm.SubItems.Add(GetProcessOwner(pro.Id));
                        index++;
                        toolStripStatusLabel3.Text = index.ToString();// view total process
                    }
                }
            }
        }

        catch (Win32Exception exp)
        {
            MessageBox.Show(exp.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void Detail_process(Process p)//start function..............
    {
        lsvitems.Items.Add("ProcessName:").SubItems.Add(p.ProcessName);
        try
        {
            lsvitems.Items.Add("BasePriority:").SubItems.Add(p.BasePriority.ToString());
        }
        catch (PlatformNotSupportedException err)
        {
            lsvitems.Items.Add("BasePriority:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("Handle:").SubItems.Add(p.Handle.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("Handle:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("Process Id:").SubItems.Add(p.Id.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("Process Id:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("MachineName:").SubItems.Add(p.MachineName);
        }
        catch (InvalidOperationException err)
        {
            lsvitems.Items.Add("MachineName:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("MainModule:").SubItems.Add(p.MainModule.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("MainModule:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("MainWindowTitle:").SubItems.Add(p.MainWindowTitle);
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("MainWindowTitle:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("MaxWorkingSet:").SubItems.Add(p.MaxWorkingSet.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("MaxWorkingSet:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("MinWorkingSet:").SubItems.Add(p.MinWorkingSet.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("MinWorkingSet:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("PagedMemorySize64:").SubItems.Add(p.PagedMemorySize64.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("PagedMemorySize64:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("PagedSystemMemorySize64:").SubItems.Add(p.PagedSystemMemorySize64.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("PagedSystemMemorySize64:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("PeakPagedMemorySize64:").SubItems.Add(p.PeakPagedMemorySize64.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("PeakPagedMemorySize64:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("PeakWorkingSet64:").SubItems.Add(p.PeakWorkingSet64.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("PeakWorkingSet64:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("PriorityClass:").SubItems.Add(p.PriorityClass.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("PriorityClass:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("FileName:").SubItems.Add(p.StartInfo.FileName);
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("FileName:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("StartTime:").SubItems.Add(p.StartTime.ToShortTimeString() + " " + p.StartTime.ToShortDateString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("StartTime:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("UserProcessorTime:").SubItems.Add(p.UserProcessorTime.TotalSeconds.ToString() + " Sec");
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("UserProcessorTime:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("PrivilegedProcessorTime:").SubItems.Add(p.PrivilegedProcessorTime.TotalSeconds.ToString() + " Sec");
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("PrivilegedProcessorTime:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("TotalProcessorTime:").SubItems.Add(p.TotalProcessorTime.TotalSeconds.ToString() + " Sec");
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("TotalProcessorTime:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("VirtualMemorySize64:").SubItems.Add(p.VirtualMemorySize64.ToString());
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("VirtualMemorySize64:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("Company Name:").SubItems.Add(p.MainModule.FileVersionInfo.CompanyName);
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("Company Name:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("FileVersion:").SubItems.Add(p.MainModule.FileVersionInfo.FileVersion);
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("FileVersion:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("LegalCopyright:").SubItems.Add(p.MainModule.FileVersionInfo.LegalCopyright);
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("LegalCopyright:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("FileDescription:").SubItems.Add(p.MainModule.FileVersionInfo.FileDescription);
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("FileDescription:").SubItems.Add(err.Message);
        }
        try
        {
            lsvitems.Items.Add("Language:").SubItems.Add(p.MainModule.FileVersionInfo.Language);
        }
        catch (Win32Exception err)
        {
            lsvitems.Items.Add("Language:").SubItems.Add(err.Message);
        }
    }

    private void Get_modules(ProcessModuleCollection prcmdlclc)
    {
        ListViewItem lstviewitm = new ListViewItem();
        lsvmodules.Items.Clear();
        foreach (ProcessModule prcmdl in prcmdlclc)
        {
            try
            {
                lstviewitm = lsvmodules.Items.Add(prcmdl.ModuleName);
                lstviewitm.SubItems.Add(prcmdl.FileVersionInfo.FileName);
                lstviewitm.SubItems.Add(prcmdl.FileVersionInfo.ProductName);
                lstviewitm.SubItems.Add(prcmdl.FileVersionInfo.ProductVersion);
            }
            catch (ArgumentException err)
            {
                MessageBox.Show(err.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    private void lsvproccess_SelectedIndexChanged(object sender, EventArgs e)
    {
        Int32 pid;
        lsvitems.Items.Clear();
        ListView lsvview = new ListView();
        if (lsvproccess.FocusedItem != null)
        {
            int index = lsvproccess.FocusedItem.Index;
            Process p = new Process();
            pid = Convert.ToInt32(lsvproccess.Items[index].SubItems[2].Text);
            p = Process.GetProcessById(pid);
            Detail_process(p);    // detailing each process in right side listview
            try
            {
                if (chbxmodule.Checked)
                    Get_modules(p.Modules);
            }
            catch (Win32Exception err)
            {
                MessageBox.Show(err.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

以管理员身份运行您的exe。 SYSTE 任务经理有同样的问题。这是因为在您不是管理员之前,您无法查看流程详细信息。