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.

Recommended Answers

All 3 Replies

first. 100 ms is way to frequent. you could save some processor usage by slowing that down to 1200ms.

2nd IsProcessOpen("notepad.exe"); according the comment in your IsProcessOpen method, it says not to use the ".exe" but instead use just "notepad"

//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.

Also you don't seem to do anything with the information after you find it.

Yeah, I tested it. It works fine. just don't put .exe on the process name.

IsProcessOpen("notepad");

another note. you might want to disable the update timer before you call the message box. otherwise you are going to end up with 10 message boxes before you get a chance to click ok.

Wow. Ishould probably stop programming late at night. Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.