c#獲取當前運行程序所在的目錄


C#獲取項目程序及運行路徑的方
1.asp.net webform用“Request.PhysicalApplicationPath獲取站點所在虛擬目錄的物理路徑,最后包含“\”;
 
2.c# winform用
A:“Application.StartupPath”:獲取當前應用程序所在目錄的路徑,最后不包含“\”;
B:“Application.ExecutablePath ”:獲取當前應用程序文件的路徑,包含文件的名稱;
C:“AppDomain.CurrentDomain.BaseDirectory”:獲取當前應用程序所在目錄的路徑,最后包含“\”;
D:“System.Threading.Thread.GetDomain().BaseDirectory”:獲取當前應用程序所在目錄的路徑,最后包含“\”;
E:“Environment.CurrentDirectory”:獲取當前應用程序的路徑,最后不包含“\”;
F:“System.IO.Directory.GetCurrentDirectory”:獲取當前應用程序的路徑,最后不包含“\”;
 
3.c# windows service服務中用“AppDomain.CurrentDomain.BaseDirectory”或“System.Threading.Thread.GetDomain().BaseDirectory”;
用“Environment.CurrentDirectory”和“System.IO.Directory.GetCurrentDirectory”將得到“ system32”目錄的路徑;
如果要使用“Application.StartupPath”或“Application.ExecutablePath ”,需要手動添加對“System.Windows.Forms.dll ”的引用,並在程序開頭用“using   System.Windows.Forms”聲明該引用;
 
4.在卸載程序獲取系統安裝的目錄:
         System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();
         string path=curPath.Location;//得到安裝程序類SetupLibrary文件的路徑,獲取這個文件路徑所在的目錄即得到安裝程序的目錄;
 
System.Diagnostics.StackFrame f = new System.Diagnostics.StackFrame(1);
                MethodBase mb = f.GetMethod();
                System.Web.HttpContext.Current.Response.Write(mb.DeclaringType.ToString()); 獲取調用類的信息,可以從父類知道子類的情況


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM