12

I'm successfully using System.Diagnostics.Process.Start() to start my external mono executable on windows. However it fails on mac. I'm not getting any error, simply nothing at all happens.

I tried doing it the following way:

System.Diagnostics.Process.Start("mono", "/path/program.exe");

Also I've tried opening terminal like the following (which also failed):

System.Diagnostics.Process.Start("Terminal");

The only thing I was able to do is launch the Terminal in the following way:

System.Diagnostics.Process.Start("open", "-a Terminal");

Any ideas? I would really appreciate any help.

Thanks!

Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
  • @konrad.kruczynski This doesn't work for me. Actually I've even tried launching the actual mono executable at "/Libraries/Frameworks/Mono.framework/Versions/2.*/bin/mono". The file is present there, however calling System.Diagnostics.Process.Start on it does nothing at all. The only thing I was able to launch with Process.Start was "open -a Terminal". – Ilya Suzdalnitski Mar 23 '13 at 14:43
  • it works fine for me, guess it's a bug then (or old Mono version or something like that). Do not hesitate to create a bug report here: http://bit.ly/qFa0pT – konrad.kruczynski Mar 24 '13 at 14:59

3 Answers3

18

What you need to do is use the full path to the actual executable file. On OSX, the "apps" are actually specially structured folders with a .app extension, and the executable (generally) lives under Content/MacOS/[name].

For example, to open the Terminal:

System.Diagnostics.Process.Start("/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal");

Or for TextEdit:

System.Diagnostics.Process.Start("/Applications/TextEdit.app/Contents/MacOS/TextEdit");

To locate the executable, you can right-click (or control-click) an app, and select Show Package Contents, and that will open up the actual folder in Finder. You can then navigate to the Contents/MacOS folder to find the actual executable.

To run your Mono executables, you have to use the full path to the mono executable and pass your program as an argument. Usually it will be something like /usr/local/bin/mono or possibly /usr/bin/mono.

For example:

System.Diagnostics.Process.Start("/usr/bin/local/mono /Users/Ilya/Projects/SomeApp.exe");

Obviously you'd use the actual path to your .exe file, the above is just an example.

DJ001
  • 75
  • 7
rossipedia
  • 56,800
  • 10
  • 90
  • 93
  • If you want to open a html page with chrome, how do you do that? I have tried System.Diagnostics.Process.Start("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome SubDB.html"); but this not work. – Carlos Borges Feb 12 '20 at 02:43
  • If chrome is your default browser, then simply `Process.Start(path/to/html/file)` should work – DJ001 Jan 15 '21 at 10:48
0

To expand on Bryan answer, please do not hardcode the path /usr/bin/local/ in your code.

To find out the correct path in each system, there are various techniques, but I'm not sure which is the best one:

  • Use which mono to locate the full path. For this you would need to call which from another Process instance. (Not sure if which comes by default in all Mac systems)
  • Use pkg-config to determine the prefix where Mono is installed. But beware, you cannot simply call system's pkg-config, you have to check first if mono's pkg-config exists. For that, you would need to check if this hardcoded path exists: /Library/Frameworks/Mono.framework/Versions/Current/bin/pkg-config (like it is done here).
knocte
  • 16,941
  • 11
  • 79
  • 125
  • On OS X, it should actually be /Libraries/Frameworks/Mono.framework/Versions/Current/bin/mono - btw. IMHO, instead of posting a separate answer you should propose an edit for things like this, where you're only adding stuff to an existing answer. – Martin Baulig Mar 18 '13 at 05:13
  • This is a good point. I would strongly suggest the `which mono` approach if this is a user application. If its a server side, then you might want to make the path to mono a configuration setting. – rossipedia Apr 13 '13 at 20:44
0

The little known feature of Mono is that you can directly run the .exe file (to be compatible with how it is done in Windows, but also for the convenience). It is started using the same Mono VM that started the original executable. In other words one can simply do:

Process.Start("Test.exe");

Well, at least it works for me on Linux ;)

Edit:

Here's the source for that feature, should work on every platform.

konrad.kruczynski
  • 46,413
  • 6
  • 36
  • 47
  • One important note: the target EXE file needs an executable bit set, or Mono will refuse the launch that program. – LubosD Jun 02 '14 at 14:54