原文: WPF 擷取程式路徑的一些方法,根據程式路徑擷取程式集資訊
一、WPF 擷取程式路徑的一些方法
方式一 應用程式域
//擷取基目錄即目前工作目錄
string str_1 = System.AppDomain.CurrentDomain.BaseDirectory;
示例結果:F:\\WPF執行個體\\bin\\Debug\\
示例說明:取得Debug目錄并且帶斜杠
//擷取應用程式基目錄的名稱
string str_2 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
方式二 通過管理應用程式
//擷取啟動了應用程式的可執行檔案的路徑,不包括可執行檔案的名稱。
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
方式四 根據目前環境和平台擷取資訊
//擷取或設定目前工作目錄的完全限定路徑。
string str_6 = System.Environment.CurrentDirectory;
//通IO的通過目錄和子目錄的靜态方法
string str_8 = System.IO.Directory.GetCurrentDirectory();
二、WPF擷取程式集詳細資訊
程式集設定圖如下:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIGOyYGM2U2YzkDZhBjMiFmNwMmNzUzYiRDMiFjNilTYfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
方式一 使用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*/