zoukankan      html  css  js  c++  java
  • .netcore打开文件:根据文件类型,调用系统对应的工具打开文件。

     研究avalonia的时候,需要在mac 和linux上用系统工具打开对应的文件。于是写了此随笔。

     .netcore 根据文件类型,调用系统对应的工具打开文件。此功能可跨平台使用(在linux,windows,macos 上均可使用)

            /// <summary>
            /// 打开指定目录下的文件
            /// </summary>
            /// <param name="filePath">文件地址(包含文件名称)</param>
            /// <returns></returns>
            public static bool OpenFile(string filePath)
            {
                //filePath = Path.Combine(Path.GetDirectoryName(typeof(App).Assembly.Location), @"ppt3.png");
                //Console.WriteLine("filePath:" + filePath);
    
                try
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))   //windows环境下打开文件
                    {
                        filePath = filePath.Replace("&", "^&");
                        Process.Start(new ProcessStartInfo("cmd", $"/c start {filePath}") { CreateNoWindow = true });
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))  //Linux环境下打开文件
                    {
                        Process.Start("xdg-open", filePath);
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))  //Mac环境下打开文件
                    {
                        Process.Start("open", filePath);
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
                return true;
            }
  • 相关阅读:
    使用nacos遇到的一些问题
    nodejs连接redis集群
    redis集群部署
    mongodb集群部署
    Mongodb删除重复数据
    docker exception
    .NET Code WebApi CentOS部署
    .NET Core 在Visual Studio Code的基本操作命令
    Mongodb对内嵌数组的增删改
    System.Web.Optimization
  • 原文地址:https://www.cnblogs.com/Fengyinyong/p/13600157.html
Copyright © 2011-2022 走看看