Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

c#: how to execute an executable from c# appliaction and log erros?

how to execute an executable from c# appliaction?

prog1.exec in c# needs to in invoke prog2.exec. prog2.exec need to return status of 0 if exec is OK and -1 if exec is not OK (prog2.exec needs to record problem in a log file. Is there a standard log that I can use).

thanks.
0
jgordin
Asked:
jgordin
  • 15
  • 10
  • 2
  • +1
4 Solutions
 
gjutrasCommented:
executable = "process1.exe";
process1 = new Process();
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.CreateNoWindow = false;
process1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process1.StartInfo.FileName = executable;
//process1.StartInfo.RedirectStandardOutput = true;
process1.Start();
process1.WaitForExit();
int exitCode = process1.ExitCode;
0
 
Vasanth SCommented:
Hi,
You can use,

Process p = System.Diagnostic.Process.Start ("Executable");
Check if p.Id has a value, and if it has then the process has been started sucessfully.

Thanks,
Vaz
0
 
jgordinAuthor Commented:
can i send an e-mail  (ms outlook) to a group of people from c#?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
YZlatCommented:
using System.Diagnostics;


string sSource="YOur Application Name";
string sLog="Application";
try
{
                                    
      Process p = new Process();
      p.StartInfo.FileName = @"YOurExecutable.exe";
      p.StartInfo.UseShellExecute = false;
      p.Start();
      
}
catch (Exception ex)
{
      if (!EventLog.SourceExists(sSource))
            EventLog.CreateEventSource(sSource,sLog);


      EventLog.WriteEntry(sSource,ex.Message + "-" + ex.StackTrace);
      EventLog.WriteEntry(sSource, ex.Message + "-" + ex.StackTrace,EventLogEntryType.Warning, 234);                              
}
0
 
jgordinAuthor Commented:
what does ....EventLogEntryType.Warning, 234 do?
0
 
gjutrasCommented:
here's a small mail sample using System.Net.Mail namespace

MailMessage email = new MailMessage();
email.To = TextBoxToEmail.Text.Replace(" ","");
email.From = TextboxFromEmail.Text.Replace(" ","");
email.Subject = "An article from World Politics Review";
email.Body = sbBody.ToString();
email.BodyEncoding = System.Text.Encoding.UTF8;
email.BodyFormat = System.Web.Mail.MailFormat.Text;
try
{
      SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["EmailSmtpServer"]!=null?System.Configuration.ConfigurationSettings.AppSettings["EmailSmtpServer"]:"localhost";
      SmtpMail.Send(email);
}
catch (Exception ex)
{                        
}
0
 
jgordinAuthor Commented:
how can i check on my computer:
1.EmailSmtpServer
2. localhost
3. any other parameters that are required
Thanks.
0
 
gjutrasCommented:
The computer your application is on needs to be able to talk to an smtp server that will let the application send outgoing email.  If you can't use the one in your outlook settings, then you can go to add/remove programs windows setup and add iis and change the details on iis and add the smtp server there.  
Once it installs, open the IIS administrative control panel and configure it to only allow relays from allowed ip's and add your ip address (spammer protection).  Then you can use localhost (you) as an smtp sender.
0
 
gjutrasCommented:
the setting you're talking about
goes in the web.config
in <appSettings></appSettings> ( make it if it's not there )
put an add key node like
<add key="EmailSmtpServer" value="smtp.boo.com" />
0
 
jgordinAuthor Commented:
what is boo?
0
 
jgordinAuthor Commented:
do i need to specify the name of the local host. if so how do i find it?
0
 
YZlatCommented:
....EventLogEntryType.Warning specifies your entry in the EventViewer as a warning
0
 
gjutrasCommented:
smtp.boo.com is a fictitious mail server name.  If your on a comcast network you could use smtp.comcast.com.  localhost is a special word to network computers that means your own internal ip address (loopback address actually, but that's not useful information for you).  So you can always refer to your own computer from your own computer as localhost.
0
 
jgordinAuthor Commented:
how can i find out on which network i am? thanks.
0
 
gjutrasCommented:
what do you mean by network?  I'd probably just recommend on your web server you install the iis smtp service and use localhost.  You'll have less configuring and issues that way.
0
 
jgordinAuthor Commented:
i am sorry. can i download iis smtp?
0
 
jgordinAuthor Commented:
related to exection:

process1.Start()

doesn't do anything. i am executing a *.exe (created c# app1) from c# app2.

i asked the same question in:

http://www.experts-exchange.com/Software/Misc/Q_22753009.html
0
 
gjutrasCommented:
see above where I mention how to install smtp.

you need to debug and figure it out use f10 to step through things.

Post your code and maybe we can help.
0
 
jgordinAuthor Commented:
         string executable = "A:\\Projects\\bin\\Release\\ABC.exe";
            Process process1                            = new Process();
            process1.StartInfo.UseShellExecute          = false;
            process1.StartInfo.CreateNoWindow           = false;
            process1.StartInfo.WindowStyle              = ProcessWindowStyle.Hidden;
            process1.StartInfo.FileName                 = executable;
            process1.Start();
            process1.WaitForExit();
            int exitCode = process1.ExitCode;
0
 
gjutrasCommented:
Your executable project is on a floppy in that folder structure?

is there a A:\\Projects\\bin\\Release\\ABC.exe ?

check task manager to see if it is actually running.  If it is and you need a window change the window style property.
0
 
jgordinAuthor Commented:
nothing happens. i tried to execute that file, *.exe, from DOS (run/cmd) screen but nothing happens either. any ideas?
0
 
jgordinAuthor Commented:
A: is the name of the drive. the directory exists.
0
 
jgordinAuthor Commented:
i set:

process1.StartInfo.UseShellExecute          = true;
 process1.StartInfo.CreateNoWindow           = true;

i can see that app2 being exec for a sec but there is no output generated by app2.
0
 
jgordinAuthor Commented:
i figured out my problem. it was app2 that was causing the proble.

i am getting a  pop up window while app2 is running. is it possible not to display that window.
0
 
gjutrasCommented:
change useshellexecute to false and if app2 isn't a window app you should be ok.
0
 
jgordinAuthor Commented:
i change useshellexecute=false but still get pop up window. any suggestions?
0
 
gjutrasCommented:
what kind of popup window
0
 
jgordinAuthor Commented:
DOS type.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 15
  • 10
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now