Start an external process on mac with c#

坚强是说给别人听的谎言 提交于 2019-12-03 03:20:32

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 (usually) lives under Content/MacOS/[name].

For example, to open Terminal:

System.Diagnosticts.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.

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).

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!