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

如何在.NET MAUI(Mac Catalyst)中打开Mac的Finder

在.NET MAUI的Mac Catalyst中打开Finder定位指定文件夹

在Mac Catalyst中没有直接对应Windows的Windows.System.Launcher.LaunchFolderPathAsync的跨平台API,需要通过平台特定代码调用Mac原生的NSWorkspace来实现。

实现步骤:

  1. 在共享项目中定义接口
    先创建一个跨平台接口用于统一调用:

    public interface IFolderOpener
    {
        void OpenFolderInFinder(string folderPath);
    }
    
  2. 在Mac Catalyst项目中实现接口
    添加平台特定的实现类,利用NSWorkspace唤起Finder并定位到目标文件夹:

    using Foundation;
    using UIKit;
    // 替换成你的项目命名空间
    [assembly: Dependency(typeof(MacFolderOpener))]
    namespace YourAppNamespace.Services
    {
        public class MacFolderOpener : IFolderOpener
        {
            public void OpenFolderInFinder(string folderPath)
            {
                var folderUrl = NSUrl.FromString(folderPath);
                if (folderUrl != null)
                {
                    // 第一个参数传null表示选中文件夹本身,第三个参数true表示激活Finder窗口
                    NSWorkspace.SharedWorkspace.SelectFile(null, folderUrl.Path, true);
                }
            }
        }
    }
    
  3. 在共享项目中调用
    通过DependencyService获取实现类并执行方法:

    var folderOpener = DependencyService.Get<IFolderOpener>();
    folderOpener.OpenFolderInFinder("/Users/YourName/TargetFolder");
    

说明:

  • NSWorkspace.SelectFile方法会打开Finder窗口,直接定位并高亮指定文件夹,完全满足“让用户查看文件存储位置”的需求。
  • 确保传入的folderPath是Mac系统的有效绝对路径,避免路径格式错误导致无法定位。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 18:50:31