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

时间:2010-02-12 22:41:53

标签: c# macos mono

我正在尝试使用Process.Start(app,args);编写一些c#代码来启动浏览器,其中apps是浏览器的路径,例如/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 <bundle identifier>] [-a <application>] [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爆炸。

我怀疑这是一个错误,但无法证明这一点。我感谢任何帮助!

4 个答案:

答案 0 :(得分:22)

要使Process.Start直接使用exec而不是使用操作系统的机制来打开文件,必须将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 });

答案 1 :(得分:4)

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

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

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

open -a Chrome

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

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

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

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

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();