如何在Mac上使用Process.Start()或等效的Mono并传入参数

我正在尝试编写一些c#代码来使用Process.Start(app,args);启动浏览器Process.Start(app,args); 应用程序是浏览器的路径,例如/Applications/Google Chrome.app/Contents/MacOS/Google Chrome和args是--no-default-browser-check

如果我这样做,它适用于Windows和Linux

 Process.Start("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","--no-first-run"); 

我明白了

 open: unrecognized option `--no-first-run' Usage: open [-e] [-t] [-f] [-W] [-n] [-g] [-h] [-b ] [-a ] [filenames] Help: Open opens files from a shell. By default, opens each file using the default application for that file. If the file is in the form of a URL, the file will be opened as a URL. Options: -a Opens with the specified application. -b Opens with the specified application bundle identifier. -e Opens with TextEdit. -t Opens with default text editor. -f Reads input from standard input and opens with TextEdit. -W, --wait-apps Blocks until the used applications are closed (even if they were already running). -n, --new Open a new instance of the application even if one is already running. -g, --background Does not bring the application to the foreground. -h, --header Searches header file locations for headers matching the given filenames, and opens them. 

我也试过Monobjc尝试运行代码

 // spin up the objective-c runtime ObjectiveCRuntime.LoadFramework("Cocoa"); ObjectiveCRuntime.Initialize(); NSAutoreleasePool pool = new NSAutoreleasePool(); // Create our process NSTask task = new NSTask(); NSPipe standardOut = new NSPipe(); task.StandardOutput = standardOut; task.LaunchPath = @"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; // add some arguments NSString argumentString = new NSString("--no-first-run"); NSArray arguments = NSArray.ArrayWithObject(argumentString); task.Arguments = arguments; // We should have liftoff task.Launch(); // Parse the output and display it to the console NSData output = standardOut.FileHandleForReading.ReadDataToEndOfFile; NSString outString = new NSString(output,NSStringEncoding.NSUTF8StringEncoding); Console.WriteLine(outString); // Dipose our objects, gotta love reference counting pool.Release(); 

但是当我使用NUnit运行我的代码时,会导致NUnit爆炸。

我怀疑这是一个错误但无法certificate。 我感谢任何和所有的帮助!

要使Process.Start直接使用exec而不是使用OS的机制来打开文件,必须将UseShellExecute设置为false。 在Linux和Windows上也是如此。

 Process.Start(new ProcessStartInfo ( "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--no-first-run") { UseShellExecute = false }); 

请注意,您还可以使用“打开”作为用例,正确运行Chrome应用包。 使用’-a’参数强制它运行特定的应用程序,’ – n’参数打开一个新实例,’ – args’传递参数:

 Process.Start(new ProcessStartInfo ( "open", "-a '/Applications/Google Chrome.app' -n --args --no-first-run") { UseShellExecute = false }); 

看起来Process使用open命令行实用程序来启动。

您应该避免直接调用可执行文件。 如果应用程序已在运行,则会启动它的第二个实例,而不是激活已在运行的实例。 这可能不是你想要的,并不是所有应用程序都能处理这个问题。

使用open,启动Chrome的语法就是

 open -a Chrome 

我不知道Process类如何在MacOS X上运行,但我认为参数应该类似。

注意,如果您只想打开网页,则不应指定可执行文件; 相反,只需传递URL,以便在用户的默认浏览器中打开它。 这适用于任何平台。

 Process.Start("http://www.google.com"); 

您是否尝试过将参数连接到进程名称而不是将其分开?

 var processName =“/ Applications / Google Chrome.app/Contents/MacOS/Google Chrome”;
 var args =“ -  no-default-browser-check”;
 Process.Start(String.Format(“{0} {1}”,processName,args));

为什么你不尝试这样的事情:

 Process P = new Process(); P.StartInfo.FileName = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; P.StartInfo.Arguments = "--no-default-browser-check"; P.UseShellExecute = false; P.Start();