• WPF 获取程序路径的一些方法,根据程序路径获取程序集信息


    一、WPF 获取程序路径的一些方法
    方式一 应用程序域

    //获取基目录即当前工作目录
    string str_1 = System.AppDomain.CurrentDomain.BaseDirectory; 
    

    示例结果:F:\WPF实例\bin\Debug\
    示例说明:取得Debug目录并且带斜杠

    //获取应用程序基目录的名称
    string str_2 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 
    

    示例结果:F:\WPF实例\bin\Debug\
    示例说明:取得Debug目录并且带斜杠

    方式二 通过管理应用程序

    //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
    string str_3 = System.Windows.Forms.Application.StartupPath; 
    

    示例结果:F:\WPF实例\bin\Debug
    示例说明:取得Debug目录不带斜杠

    //获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
    string str_4 = System.Windows.Forms.Application.ExecutablePath;
    

    示例结果:F:\WPF实例\bin\Debug\WPF实例.EXE
    示例说明:取得Debug目录下可执行程序EXE的完整路径

    方式三 本地系统进程

    //获取当前进程模块的完整路径。
    string str_5 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    

    示例结果(调试状态):F:\WPF实例\bin\Debug\WPF实例.vshost.exe
    示例结果(非调试状态):F:\WPF实例\bin\Debug\WPF实例.exe
    示例说明:取得Debug目录下可执行程序EXE的完整路径

    方式四 根据当前环境和平台获取信息

    //获取或设置当前工作目录的完全限定路径。
    string str_6 = System.Environment.CurrentDirectory; 
    

    示例结果:F:\WPF实例\bin\Debug
    示例说明:取得Debug目录不带斜杠

    //通IO的通过目录和子目录的静态方法
    string str_8 = System.IO.Directory.GetCurrentDirectory();
    

    示例结果:F:\WPF实例\bin\Debug
    示例说明:取得Debug目录不带斜杠

    二、WPF获取程序集详细信息

    程序集设置图如下:

    方式一 使用FileVersionInfo

    string filePath = System.Windows.Forms.Application.ExecutablePath;
    var versionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath);
    var FileName = versionInfo.FileName;                    //"F:\WPF实例\bin\Debug\WPF实例.EXE"
    var FileDescription = versionInfo.FileDescription;      //"WPF实例"
    var ProductName = versionInfo.ProductName;              //"WPF实例"
    var CompanyName = versionInfo.CompanyName;              //"Micro"
    var FileVersion = versionInfo.FileVersion;              //"5.6.7.8"
    var ProductVersion = versionInfo.ProductVersion;        //"5.6.7.8"
    var ProductMajorPart = versionInfo.ProductMajorPart;    //5
    var ProductMinorPart = versionInfo.ProductMinorPart;    //6
    var ProductBuildPart = versionInfo.ProductBuildPart;    //7
    var ProductPrivatePart = versionInfo.ProductPrivatePart;//8
    // 通常版本号显示为「主版本号.次版本号.生成号.专用部件号」
    var Version = String.Format("{0}.{1}.{2}.{3}", ProductMajorPart, ProductMinorPart, ProductBuildPart, ProductPrivatePart);
    var Language = versionInfo.Language;                    //"语言中性"
    var OriginalFilename = versionInfo.OriginalFilename;    //"WPF实例.exe"
    var LegalCopyright = versionInfo.LegalCopyright;        //"Copyright ©  2018"
    

    方式二 利用反射取得程序集信息

    string filePath = System.Windows.Forms.Application.ExecutablePath;
    System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(filePath);
    var assemblyName = assembly.GetName();
    string str_20 = assemblyName.Name.ToString();     //WPF实例
    string str_21 = assemblyName.FullName.ToString(); //WPF实例, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null
    string str_24 = assemblyName.Version.ToString();  //1.2.3.4
    string str_25 = assemblyName.Version.Major.ToString();          //1.2.3.4
    string str_26 = assemblyName.Version.Minor.ToString();          //1.2.3.4
    string str_27 = assemblyName.Version.Build.ToString();          //1.2.3.4
    string str_28 = assemblyName.Version.MajorRevision.ToString();  //1.2.3.4
    

    方式三 根据当前的程序集获取信息

    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
    string name = assembly.GetName().Version.ToString();
    

    方式四、获取程序集元数据, 个人推荐使用如下

    System.Reflection.AssemblyCopyrightAttribute copyright = (System.Reflection.AssemblyCopyrightAttribute)
    System.Reflection.AssemblyCopyrightAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly(),typeof(System.Reflection.AssemblyCopyrightAttribute));
    System.Reflection.AssemblyDescriptionAttribute description = (System.Reflection.AssemblyDescriptionAttribute)
    System.Reflection.AssemblyDescriptionAttribute.GetCustomAttribute(System.Reflection.Assembly.GetExecutingAssembly(),typeof(System.Reflection.AssemblyDescriptionAttribute));
    
    string str_30 = description.Description;                        // 示例描述
    string str_31 = copyright.Copyright;                            // Copyright ©  2018
    string str_32 = System.Windows.Forms.Application.ProductVersion;// 5.6.7.8*/
    

      

  • 相关阅读:
    链接脚本之LMA VMA解释
    centos6.2+nginx-1.2.3+php-5.3.17安装脚本
    _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }
    mongodb 学习笔记 04 -- 游标、索引
    WordPress中文汉字username不能注冊怎么办?
    UVALive 6665 Dragonas Cruller
    eclipse开发c++时cout和endl报错
    iOS 四种延时的方法
    有一种设计风格叫RESTful
    MySQL分区表
  • 原文地址:https://www.cnblogs.com/sntetwt/p/5401777.html
Copyright © 2020-2023  润新知