How to use Process.Start() or equivalent with Mono on a Mac and pass in arguments(如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数)
问题描述
我正在尝试编写一些 c# 代码来使用 Process.Start(app,args);
启动浏览器,其中应用程序是浏览器的路径,例如/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
和 args 是 --no-default-browser-check
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
如果我这样做,它适用于 Windows 和 Linux
If i do, which works on Windows and on 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 尝试使用
I have also tried Monobjc to try run the code with
// 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 崩溃.
But when I run my code using NUnit it causes NUnit to blow up.
我怀疑这是一个错误,但无法证明.感谢您的任何帮助!
I suspect that this is a bug but can't prove it. I appreciate any and all help!
推荐答案
要让 Process.Start 直接使用 exec 而不是使用操作系统打开文件的机制,必须将 UseShellExecute 设置为 false.在 Linux 和 Windows 上也是如此.
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(new ProcessStartInfo (
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"--no-first-run")
{ UseShellExecute = false });
请注意,您也可以对您的用例使用打开",以正确运行 Chrome 应用程序包.使用-a"参数强制它运行特定的应用程序,使用-n"参数打开一个新实例,使用--args"传入参数:
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:
Process.Start(new ProcessStartInfo (
"open",
"-a '/Applications/Google Chrome.app' -n --args --no-first-run")
{ UseShellExecute = false });
这篇关于如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01