取得程式本身執行期的路徑與檔名

沒技術性,但每次都會用到,每次都要在網路浪費一分鐘,所以記在這裡以後好找。當然啦!絕對還有超多的方法可以存取。不過,你學那麼多種方法幹嗎?

取得程式本身執行期的路徑

方法A:注意!若執行檔有被建立捷徑,則透過捷徑運行程式的當下,有可能會抓到錯誤的路徑,例如C:\Windows\System32

System.Environment.CurrentDirectory  

方法B:

System.AppDomain.CurrentDomain.BaseDirectory

方法C:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)

取得程式本身執行期的檔名

方法A:

System.AppDomain.CurrentDomain.FriendlyName

方法B:

System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)

方法C:

System.Diagnostics.Process.GetCurrentProcess().ProcessName

程式碼都幫你準備好了,自己貼上去試試最靠的住!

static void Main(string[] args)
{
  Console.WriteLine("---取出路徑---");
  Console.WriteLine(System.Environment.CurrentDirectory);
  Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
  Console.WriteLine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
  Console.WriteLine("---取出檔名---");
  Console.WriteLine(System.AppDomain.CurrentDomain.FriendlyName);
  Console.WriteLine(System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
  Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
  Console.Read();
}

※ 有用到Reflection都很耗效能,能不用就不要用。

取得ASP.NET WebForm目前運行的路徑與檔名

var oFI = new System.IO.FileInfo(Request.Url.AbsolutePath);
string cName = oFI.Name;

取得ASP.NET Url Routing後,真正運行的路徑與檔名

上面講的那個方式,到了ASP.NET MVC時代就沒有用處了,在到處都是routing的狀況下,你只會拿到rewrite過的URL而已。如果要求得真正的執行路徑(Handler),就要改用下列這個方法:

var oPage = (System.Web.UI.Page)System.Web.HttpContext.Current.CurrentHandler;
string cName = oPage.AppRelativeVirtualPath;
C# ExeSelfName ExeSelfPath Runtime ASPX ASHX ASP.NET MVC UrlRouting