.NET C#中使用Windows Explorer或者MacOS Finder打开指定文件夹

发布时间 2023-03-22 21:22:13作者: bodong

       只需要使用Process.Start启动外部进程打开指定路径即可。比如:

        private async void OnOpenAppDirectory(object sender, RoutedEventArgs e)
        {
            var directory = AppFramework.GetRuntimeApplicationDirectory();

            try
            {
                if (OperatingSystem.IsWindows())
                {
                    Process.Start("explorer.exe", $"\"{directory}\"");
                }
                else if (OperatingSystem.IsMacOS())
                {
                    Process.Start("open", $"\"{directory}\"");
                }
            }
            catch(Exception ex)
            {
                await MessageBoxUtils.ShowWarningAsync(this, "Warning", ex.Message);
            }
        }

      Windows下启动explorer.exe,MacOS下启动open 即可。