最近,同学老是用我的电脑玩war3,我就想了个办法,去检索进程列表中是否有War3.exe这个进程,然后把它给结束掉,再提示它"好好学习了".
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using Microsoft.Win32;
namespace KillProcess
... {
public partial class KillWar3 : Form
...{
public KillWar3()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.listBox1.DataSource = ShowAllProcess();
this.listBox1.SelectedIndex = -1;
this.ShowInTaskbar = false;
this.Visible = false;
this.lblStatus.Text = "一共有" + this.listBox1.Items.Count.ToString() + "进程";
//this.listBox1.dataBind()没有绑定,只需设置其数据源
if (EnableMyTimer() == true)
...{
timer1.Enabled = true;
timer1.Interval = 5000;
timer1.Start();
}
ImportDataToRegedit();
}
private void button1_Click(object sender, EventArgs e)
...{
if(KillProcess(this.listBox1.SelectedItem.ToString())==true)
MessageBox.Show("已经被杀死");
btnRefresh_Click(this, null);
}
private void btnRefresh_Click(object sender, EventArgs e)
...{
this.listBox1.DataSource = ShowAllProcess();
this.lblStatus.Text = "一共有"+this.listBox1.Items.Count.ToString()+"进程";
}
...#region
/**////列出所有进程
private ArrayList ShowAllProcess()
...{
//创建临时数组存放系统进程
ArrayList procList = new ArrayList();
//string tempName = string.Empty;
//获取每个进程
foreach (Process thisProc in System.Diagnostics.Process.GetProcesses())
...{
//获取单个进程
procList.Add(thisProc.ProcessName );
}
return procList;
}
#endregion
private void button2_Click(object sender, EventArgs e)
...{
//KillProcess("War3");
}
private void timer1_Tick(object sender, EventArgs e)
...{
bool result=KillProcess("War3");
if (result == true)
MessageBox.Show("好好学习", "war3 小提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnRefresh_Click(this, null);//每隔五秒自动刷新
}
//控制我的timer启动与否能力
private bool EnableMyTimer()
...{
int NowHour=System.DateTime.Now.Hour;
if(NowHour >=8 && NowHour <=13)
...{
return true;
}
return false;
}
/**//*
* 将要启动的应用程序路径写到注册表中具体的路径是:
HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun
只需要在上面的路径下写一个键并为此键设置一个当前应用程序的路径即开机自载
*/
private void ImportDataToRegedit()
...{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("software");
RegistryKey Microsoft = software.OpenSubKey("Microsoft");
RegistryKey Windows = Microsoft.OpenSubKey("Windows");
RegistryKey CurrentVersion = Windows.OpenSubKey("CurrentVersion");
RegistryKey run = CurrentVersion.OpenSubKey("Run",true);//一定要这个true,否则它会提示没有权限来修改注册表
foreach(string sValName in run.GetValueNames())
//开始遍历由指定子键拥有的键值名称组成的字符串数组
...{
// listBox1.Items.Add ( " " + sValName + ": " + sitekey.GetValue ( sValName ) ) ;
//在列表中加入键名称和对应的键值
if (run.GetValue(sValName).ToString() == "KillProcess")
...{
run.DeleteValue("KillProcess");
}
// RegistryKey kill = run.CreateSubKey("Kill");
run.SetValue("KillProcess",Application.ExecutablePath );// 直接在run下创建一个键值
}
}
}
}
/**/ /*
下面的代码示例演示如何在 HKEY_CURRENT_USER 下创建一个子项,处理相应的内容,然后删除该子项。
C# 复制代码
using System;
using System.Security.Permissions;
using Microsoft.Win32;
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
ViewAndModify = "HKEY_CURRENT_USER")]
class RegKey
{
static void Main()
{
// Create a subkey named Test9999 under HKEY_CURRENT_USER.
RegistryKey test9999 =
Registry.CurrentUser.CreateSubKey("Test9999");
// Create two subkeys under HKEY_CURRENT_USERTest9999. The
// keys are disposed when execution exits the using statement.
using(RegistryKey
testName = test9999.CreateSubKey("TestName"),
testSettings = test9999.CreateSubKey("TestSettings"))
{
// Create data for the TestSettings subkey.
testSettings.SetValue("Language", "French");
testSettings.SetValue("Level", "Intermediate");
testSettings.SetValue("ID", 123);
}
// Print the information from the Test9999 subkey.
Console.WriteLine("There are {0} subkeys under {1}.",
test9999.SubKeyCount.ToString(), test9999.Name);
foreach(string subKeyName in test9999.GetSubKeyNames())
{
using(RegistryKey
tempKey = test9999.OpenSubKey(subKeyName))
{
Console.WriteLine(" There are {0} values for {1}.",
tempKey.ValueCount.ToString(), tempKey.Name);
foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1}", valueName,
tempKey.GetValue(valueName).ToString());
}
}
}
using(RegistryKey
testSettings = test9999.OpenSubKey("TestSettings", true))
{
// Delete the ID value.
testSettings.DeleteValue("id");
// Verify the deletion.
Console.WriteLine((string)testSettings.GetValue(
"id", "ID not found."));
}
// Delete or close the new subkey.
Console.Write(" Delete newly created registry key? (Y/N) ");
if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
{
Registry.CurrentUser.DeleteSubKeyTree("Test9999");
Console.WriteLine(" Registry key {0} deleted.",
test9999.Name);
}
else
{
Console.WriteLine(" Registry key {0} closed.",
test9999.ToString());
test9999.Close();
}
}
}
*/
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;
using Microsoft.Win32;
namespace KillProcess
... {
public partial class KillWar3 : Form
...{
public KillWar3()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.listBox1.DataSource = ShowAllProcess();
this.listBox1.SelectedIndex = -1;
this.ShowInTaskbar = false;
this.Visible = false;
this.lblStatus.Text = "一共有" + this.listBox1.Items.Count.ToString() + "进程";
//this.listBox1.dataBind()没有绑定,只需设置其数据源
if (EnableMyTimer() == true)
...{
timer1.Enabled = true;
timer1.Interval = 5000;
timer1.Start();
}
ImportDataToRegedit();
}
private void button1_Click(object sender, EventArgs e)
...{
if(KillProcess(this.listBox1.SelectedItem.ToString())==true)
MessageBox.Show("已经被杀死");
btnRefresh_Click(this, null);
}
private void btnRefresh_Click(object sender, EventArgs e)
...{
this.listBox1.DataSource = ShowAllProcess();
this.lblStatus.Text = "一共有"+this.listBox1.Items.Count.ToString()+"进程";
}
...#region
/**////列出所有进程
private ArrayList ShowAllProcess()
...{
//创建临时数组存放系统进程
ArrayList procList = new ArrayList();
//string tempName = string.Empty;
//获取每个进程
foreach (Process thisProc in System.Diagnostics.Process.GetProcesses())
...{
//获取单个进程
procList.Add(thisProc.ProcessName );
}
return procList;
}
#endregion
private void button2_Click(object sender, EventArgs e)
...{
//KillProcess("War3");
}
private void timer1_Tick(object sender, EventArgs e)
...{
bool result=KillProcess("War3");
if (result == true)
MessageBox.Show("好好学习", "war3 小提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnRefresh_Click(this, null);//每隔五秒自动刷新
}
//控制我的timer启动与否能力
private bool EnableMyTimer()
...{
int NowHour=System.DateTime.Now.Hour;
if(NowHour >=8 && NowHour <=13)
...{
return true;
}
return false;
}
/**//*
* 将要启动的应用程序路径写到注册表中具体的路径是:
HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun
只需要在上面的路径下写一个键并为此键设置一个当前应用程序的路径即开机自载
*/
private void ImportDataToRegedit()
...{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("software");
RegistryKey Microsoft = software.OpenSubKey("Microsoft");
RegistryKey Windows = Microsoft.OpenSubKey("Windows");
RegistryKey CurrentVersion = Windows.OpenSubKey("CurrentVersion");
RegistryKey run = CurrentVersion.OpenSubKey("Run",true);//一定要这个true,否则它会提示没有权限来修改注册表
foreach(string sValName in run.GetValueNames())
//开始遍历由指定子键拥有的键值名称组成的字符串数组
...{
// listBox1.Items.Add ( " " + sValName + ": " + sitekey.GetValue ( sValName ) ) ;
//在列表中加入键名称和对应的键值
if (run.GetValue(sValName).ToString() == "KillProcess")
...{
run.DeleteValue("KillProcess");
}
// RegistryKey kill = run.CreateSubKey("Kill");
run.SetValue("KillProcess",Application.ExecutablePath );// 直接在run下创建一个键值
}
}
}
}
/**/ /*
下面的代码示例演示如何在 HKEY_CURRENT_USER 下创建一个子项,处理相应的内容,然后删除该子项。
C# 复制代码
using System;
using System.Security.Permissions;
using Microsoft.Win32;
[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
ViewAndModify = "HKEY_CURRENT_USER")]
class RegKey
{
static void Main()
{
// Create a subkey named Test9999 under HKEY_CURRENT_USER.
RegistryKey test9999 =
Registry.CurrentUser.CreateSubKey("Test9999");
// Create two subkeys under HKEY_CURRENT_USERTest9999. The
// keys are disposed when execution exits the using statement.
using(RegistryKey
testName = test9999.CreateSubKey("TestName"),
testSettings = test9999.CreateSubKey("TestSettings"))
{
// Create data for the TestSettings subkey.
testSettings.SetValue("Language", "French");
testSettings.SetValue("Level", "Intermediate");
testSettings.SetValue("ID", 123);
}
// Print the information from the Test9999 subkey.
Console.WriteLine("There are {0} subkeys under {1}.",
test9999.SubKeyCount.ToString(), test9999.Name);
foreach(string subKeyName in test9999.GetSubKeyNames())
{
using(RegistryKey
tempKey = test9999.OpenSubKey(subKeyName))
{
Console.WriteLine(" There are {0} values for {1}.",
tempKey.ValueCount.ToString(), tempKey.Name);
foreach(string valueName in tempKey.GetValueNames())
{
Console.WriteLine("{0,-8}: {1}", valueName,
tempKey.GetValue(valueName).ToString());
}
}
}
using(RegistryKey
testSettings = test9999.OpenSubKey("TestSettings", true))
{
// Delete the ID value.
testSettings.DeleteValue("id");
// Verify the deletion.
Console.WriteLine((string)testSettings.GetValue(
"id", "ID not found."));
}
// Delete or close the new subkey.
Console.Write(" Delete newly created registry key? (Y/N) ");
if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
{
Registry.CurrentUser.DeleteSubKeyTree("Test9999");
Console.WriteLine(" Registry key {0} deleted.",
test9999.Name);
}
else
{
Console.WriteLine(" Registry key {0} closed.",
test9999.ToString());
test9999.Close();
}
}
}
*/
1,为了不让别人看到我的窗体,就把this.visible设为false
2,为了一开机运行,把它加载到run下,故对注册表进行访问.
3,列出所有进程.