C# 如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2255624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Process.Start() or equivalent with Mono on a Mac and pass in arguments
提问by AutomatedTester
I am trying to write some c# code to start a browser using Process.Start(app,args);
where apps is the path to the browser e.g. /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
and the args are --no-default-browser-check
我正在尝试编写一些 c# 代码来启动浏览器,Process.Start(app,args);
其中应用程序是浏览器的路径,例如/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
,参数是--no-default-browser-check
If i do, which works on Windows and on Linux
如果我这样做,它适用于 Windows 和 Linux
Process.Start("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome","--no-first-run");
I get
我得到
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.
I have also tried Monobjcto try run the code with
我也试过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();
But when I run my code using NUnit it causes NUnit to blow up.
但是当我使用 NUnit 运行我的代码时,它会导致 NUnit 崩溃。
I suspect that this is a bug but can't prove it. I appreciate any and all help!
我怀疑这是一个错误,但无法证明。我感谢任何和所有的帮助!
采纳答案by Mikayla Hutchinson
To make Process.Start use exec directly instead of using the OS' mechanism for opening files, you must set UseShellExecute to false. This is also true on Linux and Windows.
要使 Process.Start 直接使用 exec 而不是使用操作系统的机制来打开文件,您必须将 UseShellExecute 设置为 false。在 Linux 和 Windows 上也是如此。
Process.Start(new ProcessStartInfo (
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"--no-first-run")
{ UseShellExecute = false });
Note that you can also use 'open' for your use case, to run the Chrome app bundle properly. Use the '-a' argument to force it to run a specific app, the '-n' argument to open a new instance, and '--args' to pass in arguments:
请注意,您还可以针对您的用例使用“打开”,以正确运行 Chrome 应用程序包。使用 '-a' 参数强制它运行特定的应用程序,使用 '-n' 参数打开一个新实例,并使用 '--args' 传递参数:
Process.Start(new ProcessStartInfo (
"open",
"-a '/Applications/Google Chrome.app' -n --args --no-first-run")
{ UseShellExecute = false });
回答by caesay
Why dont you try something like this:
你为什么不尝试这样的事情:
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();
回答by tucaz
Have you tried something like concatenating the parameters into the process name instead of passing it separated?
您是否尝试过将参数连接到进程名称而不是将其分开传递?
var processName = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; var args = "--no-default-browser-check"; Process.Start(String.Format("{0} {1}", processName, args));
回答by oefe
Looks like Process
uses the opencommand line utility to launch.
看起来像Process
使用开放的命令行实用程序来启动。
You should avoid calling the executable directly. If the application is already running, this would launch a second instance of it instead of activating the already running instance. That's probably not what you want, and not all applications can handle this anyway.
您应该避免直接调用可执行文件。如果应用程序已经在运行,这将启动它的第二个实例,而不是激活已经运行的实例。这可能不是您想要的,而且并非所有应用程序都可以处理此问题。
With open, the syntax to launch Chrome would be
打开后,启动 Chrome 的语法是
open -a Chrome
I don't know how the Process class works on MacOS X, but I assume that the parameters should be similar.
我不知道 Process 类在 MacOS X 上是如何工作的,但我认为参数应该是相似的。
Note, if you just want to open a web page, you should not specify the executable; instead, just pass the URL, so that it will be opened in the user's default browser. This is valid for any platform.
注意,如果你只是想打开一个网页,你不应该指定可执行文件;相反,只需传递 URL,以便在用户的默认浏览器中打开它。这对任何平台都有效。
Process.Start("http://www.google.com");