gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:24:43 26 4
gpt4 key购买 nike

我正在尝试编写一些 c# 代码以使用 Process.Start(app,args); 启动浏览器,其中 apps 是浏览器的路径,例如/Applications/Google Chrome.app/Contents/MacOS/Google Chrome 参数是 --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 崩溃。

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

最佳答案

要使 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 });

关于c# - 如何在 Mac 上使用 Process.Start() 或等效的 Mono 并传入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2255624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com