簡體   English   中英

通過C#運行應用程序的無處不在的方式來獲取根目錄

[英]Ubiquitous way to get the root directory an application is running in via C#

我的應用程序要求我可以訪問文件系統。 無論代碼是在Web服務器還是控制台應用程序上運行,我如何獲得運行應用程序的根目錄。像C:\\ TheApplication ...

謝謝!

最簡單的可能是:

System.Reflection.Assembly.GetExecutingAssembly().Location

希望這可以幫助,

-Oisin

你可以試試這個:

String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

您可以在這里閱讀有關這兩個類的更多信息:

我剛剛測試了一種非常基本的方法,可以在asp.net和windows服務中運行。

var binariesPath = string.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath) 
                    ? AppDomain.CurrentDomain.BaseDirectory // Windows Service
                    : AppDomain.CurrentDomain.RelativeSearchPath; // Asp.net

更簡單:

 string myPath = Application.ExecutablePath; 

對於winforms應用程序,始終設置此項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM