The killing process under C #

Source: Internet
Author: User
Tags save file tostring

. NET encapsulates the process management very well, by using the System.Diagnostics.Process.GetProcesses () method to get an array of processes within the system. Enumeration allows you to get all the processes within your system (see more than the process browser that Windows takes).

The ProcessName property gets the prefix of the filename, Notepad, and does not contain the following. exe.

To get more information, you need to get an array of modules properties, but not every process can get the Modules property, so you must use a try ... The catch statement excludes exceptions. The first member of the array [0]. FileName contains the full file name of the executing program, equivalent to Processmodule; a member with an ordinal number greater than 0 often contains DLL information called by the program, and the result is a bit like a Trojan horse program.

The Kill () method is used to kill the process, and the killed program does not pop up a dialog box similar to the save file, and is a very cold-blooded quick hand.

The StartInfo property is used to start a new process and is not intended to retrieve a running process.

private void Btnprocess_click (object sender, System.EventArgs e)
{
System.Diagnostics.Process [ ]myps;
Myps=system.diagnostics.process.getprocesses ();
This.richTextBox1.Clear ();           
foreach (System.Diagnostics.Process p in Myps)
{
if (p.id!=0)
{
String mys= "process name" +p.processname+ "ID:" +p.id.tostring ();               
try//because processes are different, some processes do not contain Modules information, so try to protect
{
if (p.modules!=null)
if (p.modules.count>0)
{
System.Diagnostics.ProcessModule pm=p.modules[0];

myS+="\n Modules[0].FileName:"+pm.FileName;
                myS+="\n Modules[0].ModuleName:"+pm.ModuleName;
                myS+="\n Modules[0].FileVersionInfo:\n"+pm.FileVersionInfo.ToString();
                if (pm.FileName.ToLower()==this.textBox1.Text.Trim().ToLower())
                  p.Kill();
              }
          }
          catch
          {}
          finally
          {
            this.richTextBox1.Text += myS+"\n==========================\n";
          }
        }
      }

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.