C#中AppDomain.CurrentDomain.BaseDirectory與Application.StartupPath的區別

// 獲取程序的基目錄。
System.AppDomain.CurrentDomain.BaseDirectory
 
// 獲取模塊的完整路徑。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
 
// 獲取和設置當前目錄(該進程從中啓動的目錄)的完全限定目錄。
System.Environment.CurrentDirectory
 
// 獲取應用程序的當前工作目錄。
System.IO.Directory.GetCurrentDirectory() 

// 獲取和設置包括該應用程序的目錄的名稱。
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
 
// 獲取啓動了應用程序的可執行文件的路徑。
System.Windows.Forms.Application.StartupPath 

// 獲取啓動了應用程序的可執行文件的路徑及文件名
System.Windows.Forms.Application.ExecutablePath
 
 
 
C# WinForm中AppDomain.CurrentDomain.BaseDirectory與Application.StartupPath的區別示例如下:
 
private void Frm_Server_Load(object sender, EventArgs e)
        {
            MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory);
            MessageBox.Show(Application.StartupPath );
         }
 
說明:
 
1.   AppDomain.CurrentDomain.BaseDirectory 返回結果爲: D:\mycode\
 
     Application.StartupPath 返回結果爲: D:\mycode
 
2.  Application.StartupPath 只能用於WinForm窗體中,而AppDomain.CurrentDomain.BaseDirectory既可以用於WinForm窗體中,也可以用於類庫DLL文件中.
3. HttpContext.Current.Request.Url.Host.ToString() 返回結果爲:http://locahost.XXXX.XXX
   HttpContext.Current.Server.MapPath();
   System.Web.HttpContext.Current.Request.PhysicalApplicationPath

<pre class="csharp" name="code">//Silverlight獲得站點根目錄:
var _xapUri = Application.Current.Host.Source;
var _webUri = new Uri(_xapUri, "../");



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章