使用 Google 搜尋本站文章:
首頁 | CodeCharge 討論板 | 部落格 | ASP.NET 電子書 | 所有文章 | 相簿 | News | 網站導讀 | 聯絡 Allen | 訂閱 RSS
Microsoft MVP 
since 2004 
 

Blog 分類 :


如何取得目前程式執行的根目錄

By Allen Kuo, 建立日期:2009/09/12 14:30
說明Console Application, Windows Forms, ASP.NET的共用寫法
    加入書籤: 收進你的MyShare個人書籤

若在 asp.net 裡想取得根目錄的實體位置,可以寫成
string path = Server.MapPath("/");

若在 Windows Forms可以寫成
string path = Application.StartupPath ;

若在 Console Application 可以寫成
string path=System.AppDomain.CurrentDomain.BaseDirectory;

如果您想寫一支 dll 專案,供上述專案類型參考並叫用, 而您想在 dll 取得目前該專案的根目錄, 則可以寫成
string path=System.AppDomain.CurrentDomain.BaseDirectory;

是可以在上述三種專案裡同時正確執行。


Copyright © AllenKuo.com , 2000 - 2010