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

摘要:
stringstr_4=System.Windows.Forms.Application。可执行路径;示例结果:F:WPF实例bin调试WPF实例EXE示例描述:获取Debug目录下可执行EXE的完整路径//获取当前流程模块的完整路径。

一、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获取程序集详细信息

程序集设置图如下:

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

方式一 使用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*/

免责声明:文章转载自《WPF 获取程序路径的一些方法,根据程序路径获取程序集信息》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Mybatis,返回Map的时候,将Map内的Key转换为驼峰的命名(转)示例化讲解RIP路由更新机制下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

yii2源码学习笔记(十四)

Module类是模块和应用类的基类。  yiisoftyii2aseModule.php 1 <?php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright Copyright (c) 2008 Yii Software LLC 5 * @license h...

matlab 中如何创建以及获取popupmenu的值

1.如何创建popupmenu的值 如图,点击河南左边的符号,会弹出右边的小窗口,输入完一项之后点击enter继续创建第二项即可。 2.如何获取popupmenu的值 functionpopupmenu_Callback(hObject, eventdata, handles) val = get(handles.popupmenu,'value...

容器编排系统K8s之包管理器Helm基础使用(一)

前文我们了解了k8s上的hpa资源的使用,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14293237.html;今天我们来聊一下k8s包管理器helm的相关话题; helm是什么? 如果我们把k8s的资源清单类比成centos上的rpm包,那么helm的作用就如同yum;简单讲helm就是类似yum这样的包管理...

python图像处理之pyocr

使用pyocr类库进行ocr识别,其中tools为’Tesseract’ #!/usr/bin/env python #coding=utf-8 __author__ = 'zhangdebin' from PIL import Image import sys import pyocr tools = pyocr.get_available_to...

WPF读写config配置文件

WPF读写config配置文件单。 1. 在你的工程中,添加app.config文件。文件的内容默认为: 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 </configuration> 2.如果你想给程序配置一些参数,就在<c...

WPF DataGrid绑定到数据源的方法

1 string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["str"].ConnectionString; 2 SqlConnection con = new SqlConnection(conStr); 3...