//獲取包含清單的已加載文件的路徑或 UNC 位置。
public static string sApplicationPath = Assembly.GetExecutingAssembly ( ).Location; //result: X:\xxx\xxx\xxx.dll (.dll文件所在的目錄+.dll文件名)
//獲取當前進程的完整路徑,包含文件名(進程名)。 string str = this.GetType ( ).Assembly.Location; //result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)
//獲取新的 Process 組件並將其與當前活動的進程關聯的主模塊的完整路徑,包含文件名(進程名)。 string str = System.Diagnostics.Process.GetCurrentProcess ( ).MainModule.FileName; //result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)
//獲取和設置當前目錄(即該進程從中啓動的目錄)的完全限定路徑。 string str = System.Environment.CurrentDirectory; //result: X:\xxx\xxx (.exe文件所在的目錄)
//獲取當前 Thread 的當前應用程序域的基目錄,它由程序集衝突解決程序用來探測程序集。 string str = System.AppDomain.CurrentDomain.BaseDirectory; //result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")
//獲取和設置包含該應用程序的目錄的名稱。 string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")
//獲取啓動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。 string str = System.Windows.Forms.Application.StartupPath; //result: X:\xxx\xxx (.exe文件所在的目錄)
//獲取啓動了應用程序的可執行文件的路徑,包括可執行文件的名稱。 string str = System.Windows.Forms.Application.ExecutablePath; //result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)
//獲取應用程序的當前工作目錄(不可靠)。 string str = System.IO.Directory.GetCurrentDirectory ( ); //result: X:\xxx\xxx (.exe文件所在的目錄)
在系統服務中最好用這個方式去取路徑 string stmp = Assembly.GetExecutingAssembly ( ).Location; stmp = stmp.Substring ( 0 , stmp.LastIndexOf ( '\\' ) );//刪除文件名 if ( pathType == 1 ) return stmp + @"\inputLog.xml"; else if ( pathType == 2 ) return stmp + @"\MiddleDB.xml"; else return stmp + @"\AppNo.xml";
using System.IO; string path = "d:asdfasdf.bmp"; string fileName = Path.GetFileName(path); //文件名 string ext = Path.GetExtension(path); //擴展名
1、判定一個給定的路徑是否有效,合法 2、如何確定一個路徑字符串是表示目錄還是文件 9、文件夾瀏覽對話框(FolderBrowserDialog類) 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName-獲取模塊的完整路徑。 2.System.Environment.CurrentDirectory-獲取和設置當前目錄(該進程從中啓動的目錄)的完全限定目錄。 3.System.IO.Directory.GetCurrentDirectory()-獲取應用程序的當前工作目錄。這個不一定是程序從中啓動的目錄,有可能程序放在C:\xxx裏,這個函數有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時不一定返回什麼。 4.System.AppDomain.CurrentDomain.BaseDirectory-獲取程序的基目錄。 5.System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase-獲取和設置包括該應用程序的目錄的名稱。 6. System.Windows.Forms.Application.StartupPath-獲取啓動了應用程序的可執行文件的路徑。效果和2、5一樣。只是5返回的字符串後面多了一個"\"而已 7.System.Windows.Forms.Application.ExecutablePath-獲取啓動了應用程序的可執行文件的路徑及文件名,效果和1一樣。對於Windows程序 和Web 應用程序來說,他們運行的路徑是不一樣的,所以關鍵是判斷當前運行的程序是哪種程序.於是我們可以使用如下的代碼 string path = ""; if (System.Environment.CurrentDirectory ==AppDomain.CurrentDomain.BaseDirectory)//Windows應用程序則相等 ...{path = AppDomain.CurrentDomain.BaseDirectory; }else...{path = AppDomain.CurrentDomain.BaseDirectory + "Bin\"; }這樣如果我們寫了一個類庫,類庫中用到了Assembly.LoadFrom,由於是通用類庫,所以可能用到Windows程序中也可能用到Web中,那麼用上面的代碼就很方便了. 1、Server.MapPath 2、System.Windows.Forms.StartupPath 3、Type.Assembly.Location方法2可以應用於控制檯應用程序,WinForm應用程序,Windows服務,方法1可以應用於Web應用程序,方法3都可以應用。但方法3是加載應用程序的路徑。如果是Web應用程序,取得的路徑是:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files目錄。所以Web項目還是使用Server.MapPath吧。否則建議使用方法2。如果自己新建類庫。可以加入對System.Windows.Forms.StartupPath的引用後使用。 |
C# 獲取當前路徑方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.