You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Mac OS中通过C#打开文件?解决找不到文件报错

在MacOS上用C#打开指定文件的解决方案

问题场景

需要在MacOS环境下通过C#代码打开指定文件(比如系统自带Terminal的二进制文件/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal),原代码尝试直接调用System.Diagnostics.Process.Start()但抛出文件找不到的错误:

原测试代码

namespace democonsole
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Process.Start("/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal");
        }
    }
}

报错信息

Unhandled Exception:
System.ComponentModel.Win32Exception (0x80004005): Cannot find the specified file
  at System.Diagnostics.Process.StartWithShellExecuteEx (System.Diagnostics.ProcessStartInfo startInfo) [0x00102] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at System.Diagnostics.Process.Start () [0x00032] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.Start()
  at System.Diagnostics.Process.Start (System.Diagnostics.ProcessStartInfo startInfo) [0x0001b] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at System.Diagnostics.Process.Start (System.String fileName) [0x00006] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at democonsole.Program.Main (System.String[] args) [0x0000c] in <1d59817ec01e4193b472af562e50f6f2>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ComponentModel.Win32Exception (0x80004005): Cannot find the specified file
  at System.Diagnostics.Process.StartWithShellExecuteEx (System.Diagnostics.ProcessStartInfo startInfo) [0x00102] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at System.Diagnostics.Process.Start () [0x00032] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.Start()
  at System.Diagnostics.Process.Start (System.Diagnostics.ProcessStartInfo startInfo) [0x0001b] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at System.Diagnostics.Process.Start (System.String fileName) [0x00006] in <6de7aa624f1544329fc30bc179f3b197>:0 
  at democonsole.Program.Main (System.String[] args) [0x0000c] in <1d59817ec01e4193b472af562e50f6f2>:0 

错误原因

  1. 在Mono环境下,Process.Start()默认使用ShellExecute模式,该模式在MacOS上的行为与Windows差异较大,无法正确解析应用包内部的二进制文件路径。
  2. 直接调用应用包内的二进制文件不符合MacOS的应用启动规范,容易出现权限或环境变量缺失的问题。

解决方法

方法1:使用MacOS系统open命令(推荐)

open是MacOS原生的文件/应用启动工具,能自动处理应用包结构,兼容性最好。

代码示例

namespace democonsole
{
    class Program
    {
        static void Main(string[] args)
        {
            // 启动Terminal应用
            var startInfo = new System.Diagnostics.ProcessStartInfo
            {
                FileName = "/usr/bin/open",
                // 参数:-a 指定应用名称,或直接传入应用路径/文件路径
                Arguments = "-a Terminal",
                UseShellExecute = false,
                CreateNoWindow = true
            };
            System.Diagnostics.Process.Start(startInfo);

            // 打开普通文件示例:
            // Arguments = "/Users/yourname/Documents/test.txt";
        }
    }
}

方法2:直接调用二进制文件(需路径验证)

如果必须直接调用应用包内的二进制文件,需先验证路径有效性,并关闭UseShellExecute模式。

代码示例

namespace democonsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string targetPath = "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal";
            
            // 先检查文件是否存在
            if (System.IO.File.Exists(targetPath))
            {
                var startInfo = new System.Diagnostics.ProcessStartInfo
                {
                    FileName = targetPath,
                    UseShellExecute = false,
                    CreateNoWindow = false
                };
                System.Diagnostics.Process.Start(startInfo);
            }
            else
            {
                System.Console.WriteLine("指定路径的文件不存在");
            }
        }
    }
}

验证方法

编译代码后,使用mono命令执行生成的exe文件,即可正常启动目标应用或打开指定文件。

内容的提问来源于stack exchange,提问作者Big Bored

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 19:46:18