這兩個計划寫一個小類庫,需要在不同項目下任意調用。該類庫需要對磁盤文件進行讀寫,所以就需要獲取程序執行的磁盤路徑,就簡單的對獲取磁盤路徑的方法進行研究。
借助搜索引擎,我從網上搜羅來多種方法,都可以直接或間接的獲取到應用程序執行的根目錄。大概總結一下,一共有以下 11 種:
Server.MapPath("~") //使用 HTTP 上下文中的 Server 對象來獲取Web站點的根目錄 System.AppDomain.CurrentDomain.BaseDirectory //使用應用程序域對象獲取當前線程的應用程序域的基准目錄 System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase //使用應用程序域對象獲取當前線程的應用程序域的配置信息中的應用程序目錄 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName //獲取當前進程的主模塊的文件名(全路徑。由該文件路徑可以得到程序集所在的目錄) System.Environment.CurrentDirectory //獲取應用程序環境的當前目錄 System.IO.Directory.GetCurrentDirectory() //使用靜態類 Directory 下的 GetCurrentDirectory 方法獲取當前程序的路徑 System.Reflection.Assembly.GetCallingAssembly().Location //獲取調用該方法的方法所在的程序集,並獲取該程序集文件路徑(由該文件路徑可以得到程序集所在的目錄) System.Reflection.Assembly.GetEntryAssembly().Location //獲取包含該應用程序入口點的程序集(可執行文件),並獲取該程序集文件的路徑(由該文件路徑可以得到程序集所在的目錄) System.Reflection.Assembly.GetExecutingAssembly().Location //獲取執行該方法的程序集,並獲取該程序集的文件路徑(由該文件路徑可以得到程序集所在的目錄) System.Windows.Forms.Application.StartupPath //獲取啟動應用程序的可執行文件所在的目錄 System.Windows.Forms.Application.ExecutablePath //獲取啟動應用程序的可執行文件的路徑(由該文件路徑可以得到應用程序所在的目錄)
當然,這些方法並不全都可用。
要在類庫里使用,當然要在類庫里試試。
新建一個解決方案,添加一個類庫項目,然后再添加一個控制台項目和 Web 項目。項目結構如圖:
首先,在類庫項目里添加一個類 ProjectPath。代碼如下:
namespace Common { public class ProjectPath { public static Dictionary<string, string> GetCurrentPaths() { Dictionary<string, string> paths = new Dictionary<string, string>(); paths.Add("System.AppDomain.CurrentDomain.BaseDirectory", System.AppDomain.CurrentDomain.BaseDirectory); paths.Add("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase", System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase); paths.Add("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName", System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); paths.Add("System.Environment.CurrentDirectory", System.Environment.CurrentDirectory); paths.Add("System.IO.Directory.GetCurrentDirectory()", System.IO.Directory.GetCurrentDirectory()); paths.Add("System.Reflection.Assembly.GetCallingAssembly().Location", System.Reflection.Assembly.GetCallingAssembly().Location); try {/* System.Reflection.Assembly.GetEntryAssembly() 方法被 Web 應用程序調用時返回值是 null, 訪問其 Location 屬性將產生異常 */ paths.Add("System.Reflection.Assembly.GetEntryAssembly().Location", System.Reflection.Assembly.GetEntryAssembly().Location); } catch { } paths.Add("System.Reflection.Assembly.GetExecutingAssembly().Location", System.Reflection.Assembly.GetExecutingAssembly().Location); paths.Add("System.Windows.Forms.Application.StartupPath", System.Windows.Forms.Application.StartupPath); paths.Add("System.Windows.Forms.Application.ExecutablePath", System.Windows.Forms.Application.ExecutablePath); //Web 項目特有方式,必需在 HTTP 上下文中才能訪問 Server 對象 //paths.Add("Server.MapPath(\"~\")", Server.MapPath("~")); return paths; } } }
在段代碼里,ProjectPath 類提供一個 GetCurrentPaths 方法來返回使用各種方式獲取程序目錄結果的字典。
OK,先在控制台項目里測試,代碼如下:
class Program { static void Main(string[] args) { Dictionary<string, string> paths = ProjectPath.GetCurrentPaths(); foreach (string key in paths.Keys) { Console.WriteLine(key); Console.WriteLine(paths[key]); Console.WriteLine(); } Console.Read(); } }
執行以后,將在控制台打印執行結果,如圖:
從執行結果來講,控制台項目(包括 WindowsForm 應用程序)使用任何方式都能直接或間接獲取程序集所在的目錄。不過從結果也能看出,各種方法執行的功能是不一樣的。
接下來,在 Web 項目中測試,代碼如下:
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.form1.InnerHtml += "Server.MapPath(\"~\")<br />"; this.form1.InnerHtml += Server.MapPath("~"); this.form1.InnerHtml += "<br />"; this.form1.InnerHtml += "<br />"; Dictionary<string, string> paths = ProjectPath.GetCurrentPaths(); foreach (string key in paths.Keys) { this.form1.InnerHtml += key; this.form1.InnerHtml += "<br />"; this.form1.InnerHtml += paths[key]; this.form1.InnerHtml += "<br />"; this.form1.InnerHtml += "<br />"; } } }
運行 Web 項目,執行結果如圖:
同樣的代碼,Web 項目的執行結果大不一樣。可以說亂七八糟。
首先,Server.MapPath() 方法不容質疑,是 Web 程序提供的特有方法。其它的返回的要不是生成的 DLL 文件所在的臨時目錄,要不就是 VS 集成的Web服務器所在的目錄。只有 System.AppDomain.CurrentDomain.BaseDirectory 屬性和 System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase 屬性能夠順利得到Web應用程序運行的目錄。