0

I am trying to execute a console application developed in C++, although in C# when I use a method such as:

private void StartProcess()
{
Process.Start("consoleapp.exe");
}

It does not appear or even seem to execute, no exceptions thrown etc.

Is there a better way to do this?

James
  • 1,459
  • 3
  • 16
  • 15
  • If there's no exception then the process was launched. Are you sure `consoleapp.exe` isn't just immediately closing for some reason? – Sven Jul 16 '11 at 16:28
  • @Sven That's exactly what I thought at first, but when run normally e.g. by double clicking on the executable file it opens up a window fine – James Jul 16 '11 at 16:32
  • Maybe you could use a tool like sysinternals Process Monitor to figure out what's going on. I also suggest taking a look at the `Process` instance returned by the `Process.Start` method. Pay attention to properties such as `HasExited` and `ExitCode`. – Sven Jul 16 '11 at 16:35

2 Answers2

0

Is consoleapp.exe in the same directory as your c# executable or in the path?

There is a good description of how to do this in the following article:

How To: Execute command line in C#, get STD OUT results

Community
  • 1
  • 1
iandotkelly
  • 9,024
  • 8
  • 48
  • 67
  • It's in the same directory as the executable – James Jul 16 '11 at 16:28
  • Well, it could be an issue of directing standard output. I would have thought that by default it would pop up a window like double-clicking the file in explorer, but I would follow the example where it redirects stdout and then reads the stream. – iandotkelly Jul 16 '11 at 16:39
0

Yes, try doing it like this,

using System;
using System.Diagnostics;

class ProcessStart{
    static void Main(string[] args){

        Process sample = new Process();

        sample.StartInfo.FileName   = "sample.exe";
        sample.Start();
    }
}

the key code is in the main function, just make sure the exe is in the same directory, and include the

using System.Diagnostics; 

at the top.

this code works for me and seems the most correct way. hope i helped.

    Process sample = new Process();

    sample.StartInfo.FileName   = "sample.exe";
    sample.Start();

just change the Process sample to w/e you want, so sample can be w/e you want.

you may also pass arguments to the exe using this.

sample.StartInfo.Arguments = "sample argument";

just put this before the starting the exe part.

hope i helped :)

TheEliteNoob
  • 94
  • 1
  • 10
  • Your code is identical in function to the OP's code, just more verbose. It will not solve his problem. – Sven Jul 16 '11 at 16:34
  • you sure? I just thought this was different, he also doesn't seem to be declaring a new process. Therefore, i offered mine. however thanks for telling me. – TheEliteNoob Jul 16 '11 at 16:40
  • The static `Process.Start` methods return a new instance. It's just a shorthand way of doing what you're doing. – Sven Jul 16 '11 at 16:41