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*/

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM