-2

I'm calling a console application from a Web Form which sends out an email blast. In the finally block of the console code, a confirmation email is sent with a count of successful emails.

During runtime, two confirmation mails are being generated instead of one; the first one always has 0 email count, and the second one has the actual count.

Here is the calling code in the Web Form:

ProcessStartInfo info = new ProcessStartInfo();

string arguments = String.Format(@"""{0}"" ""{1}""", 
    message.Subject.Replace(@"""", @""""""), 
    message.Body.Replace(@"""", @""""""));
info.FileName = MAILER_FILEPATH;

Process process = Process.Start(info.FileName, arguments);
Process.Start(info);

What am I doing wrong?

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91

1 Answers1

2

The problem you're having is that you're calling Process.Start twice; once with the info object (but without arguments), and the next with the filename and arguments overload.

You have two methods for starting a process shown; you should pick one and stick with it:

ProcessStartInfo info = new ProcessStartInfo();

string arguments = String.Format(@"""{0}"" ""{1}""", 
    message.Subject.Replace(@"""", @""""""), 
    message.Body.Replace(@"""", @""""""));
info.FileName = MAILER_FILEPATH;
info.Arguments = arguments;

Process.Start(info);

ProcessStartInfo includes a property called Arguments that you can add the arguments to; and there's an overload of Process.Start that just takes a ProcessStartInfo object. That's the method I describe above.

George Stocker
  • 57,289
  • 29
  • 176
  • 237