代码之家  ›  专栏  ›  技术社区  ›  AutomatedTester

如何在Mac上使用process.start()或等效的mono并传入参数

  •  23
  • AutomatedTester  · 技术社区  · 16 年前

    我正在尝试编写一些C代码来启动浏览器 Process.Start(app,args); 其中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 回复  |  直到 13 年前
        1
  •  21
  •   Mikayla Hutchinson    13 年前

    若要创建进程,请直接启动use exec,而不是使用操作系统的机制打开文件,必须将useShellExecute设置为false。在Linux和Windows上也是如此。

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

    请注意,您也可以将“open”用于您的用例,以正确运行chrome应用程序包。使用“-a”参数强制它运行特定的应用程序,使用“-n”参数打开新实例,使用“--args”传入参数:

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

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

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

    使用open,启动chrome的语法是

    open -a Chrome
    

    我不知道进程类在MacOSX上是如何工作的,但是我假设参数应该是相似的。

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

    Process.Start("http://www.google.com");
    
        3
  •  1
  •   tucaz    16 年前

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

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

    你为什么不试试这样的东西:

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