201910041337取得目前程式執行的根目錄

在 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;

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

回應