Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to determine if ms outlook is running before sending an email but i cant seem to get it right. my code is shown below. Any help will be welcomed
C#
bool msOutlook = false;
foreach(Process otlk in Process.GetProcesses())
                {
                    if(otlk.ProcessName.Contains("outlook"))
                    {
                        msOutlook = true;
                    }
                }
                if (msOutlook.Equals(true))
                {
                    Mail.Send();
                    MessageBox.Show("Applicant has been forwarded", "Forward Applicant", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("CV not forwarded. An instance of Ms Outlook needs to be running to send email", "Mail error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
Posted
Updated 11-Apr-11 15:06pm
v3

Come on, another weird way of assigning and checking Boolean!

Instead of

C#
if (otlk.ProcessName.Contains("outlook"))
{
      msOutlook = true;
}
//...
if (msOutlook.Equals(true)) { //... wow! -- SA


must be:

C#
bool isOutlook;

//...
isOutlook = otlk.ProcessName.Contains("outlook");

//...

if (isOutlook) //...


This is just a side node: such quality of code as above demonstrates confusion about Boolean and simply unacceptable.

Now, the problem can be simply the case of the string.

Try instead:

C#
isOutlook = otlk.ProcessName.ToLower().Contains("outlook");


After all, run Outlook, run this code under debugger, and identify the process representing Outlook "manually" looking the variable through debugger. Browse this instance of the Process and find out what differs it from other processes, build you check accordingly.

—SA
 
Share this answer
 
You may simplify your code like way:

C#
if (Process.GetProcessesByName("Outlook.exe").Length > 0)
            {
                Mail.Send();
                MessageBox.Show("Applicant has been forwarded", "Forward Applicant", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("CV not forwarded. An instance of Ms Outlook needs to be running to send email", "Mail error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900