Net中獲取程序集路徑

 

從內存中加載的程序集,無路徑

 

IIS中路徑

protected void Page_Load(object sender, EventArgs e)

{

Response.Write("程序集路徑"+System.Reflection.Assembly.GetExecutingAssembly().Location+"<BR/>");

Response.Write("程序域路徑"+System.AppDomain.CurrentDomain.BaseDirectory + "<BR/>");

Response.Write("模塊路徑"+System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName + "<BR/>");

Response.Write("運行時路徑"+HttpRuntime.BinDirectory + "<BR/>");

}

 

 

 

輸出

 

 

程序集路徑C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\test\02a51a22\1e6b2246\assembly\dl3\3464d812\41e5e6a4_2697d301\WebApplication3.dll
程序域路徑d:\Users\zwx\Documents\visual studio 2015\Projects\WebApplication3\WebApplication3\
模塊路徑c:\windows\system32\inetsrv\w3wp.exe
運行時路徑d:\Users\zwx\Documents\visual studio 2015\Projects\WebApplication3\WebApplication3\bin\

 

 

 

WINFORM中路徑

Console.Write("程序集路徑" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\r\n");

Console.Write("程序域路徑" + System.AppDomain.CurrentDomain.BaseDirectory + "\r\n");

Console.Write("模塊路徑" + System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName + "\r\n");

Console.Write("運行時路徑" + System.Windows.Forms.Application.ExecutablePath+ "\r\n");

 

輸出

程序集路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bi

n\Debug\ZY.Cloud.Front.GatewayService.exe

程序域路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bi

n\Debug\

模塊路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bin\

Debug\ZY.Cloud.Front.GatewayService.exe

運行時路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bi

n\Debug\ZY.Cloud.Front.GatewayService.exe

 

 

服務中路徑

程序集路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bin\Debug\ZY.Cloud.Front.GatewayService.exe

程序域路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bin\Debug\

模塊路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bin\Debug\ZY.Cloud.Front.GatewayService.exe

運行時路徑D:\CodeCard\前置伺服器\綜合前置伺服器\ZY.Cloud.Front.GatewayService\bin\Debug\ZY.Cloud.Front.GatewayService.exe

 

 

 

操作文件時,最好在代碼中顯示指定路徑,畢竟程序集自身的位置是不可控的。推薦使用程序域路徑或ExecutablePath

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章