I am creating a program that password protects software. The way I plan to protect it is to check if the program is running, and if it is, open up a window asking for a password. If it is wrong, close the protected software. I have this piece of code to check for the software process:
private void update_Tick(object sender, EventArgs e)
{
protecting.Text = "Protecting: " + Settings.Default.exe.ToString();
if (protecting.Text == "Protecting: ")
{
protecting.Text = "Protecting: Nothing";
}
status.Text = Settings.Default.status.ToString();
if (status.Text == "Disabled")
{
status.ForeColor = Color.Red;
}
else
{
status.ForeColor = Color.Green;
}
//Problem part
IsProcessOpen("notepad.exe");
}
public bool IsProcessOpen(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
//now we're going to see if any of the running processes
//match the currently running processes. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running.
//Remember, if you have the process running more than once,
//say IE open 4 times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
if (clsProcess.ProcessName.Contains(name))
{
//if the process is found to be running then we
//return a true
MessageBox.Show("You are not allowed to use this program...");
update.Enabled = false;
return true;
}
}
update.Enabled = true;
//otherwise we return a false
return false;
}
}
But whenever I have that running and I start up notepad, it doesn't do anything. Oh yeah, and update is a timer that ticks ever 100 milliseconds. It's enabled too.