C# 之 獲取文件名及拓展名 1、用Path類的方法(最常用) string fullPath = @"\WebSite\Default.aspx"; string filename = System.IO.Path.GetFileName(fullPath);//帶拓展名的文件名 “Default ...
C# 之 獲取文件名及拓展名
1、用Path類的方法(最常用)
string fullPath = @"\WebSite\Default.aspx";
string filename = System.IO.Path.GetFileName(fullPath);//帶拓展名的文件名 “Default.aspx”
string extension = System.IO.Path.GetExtension(fullPath);//擴展名 “.aspx”
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(fullPath);// 不帶擴展名的文件名 “Default”
string dir = System.IO.Path.GetDirectoryName(fullPath); //返迴文件所在目錄“\\WebSite”
2、用Substring截取
string aFirstName = fullPath.Substring(fullPath.LastIndexOf("\\") + 1, (fullPath.LastIndexOf(".") - fullPath.LastIndexOf("\\") - 1)); //不帶拓展名的文件名“Default”
string aLastName = fullPath.Substring(fullPath.LastIndexOf(".") + 1, (fullPath.Length - fullPath.LastIndexOf(".") - 1)); //擴展名“aspx”
string strFileName = fullPath.Substring(fullPath.LastIndexOf("\\") + 1, fullPath.Length - 1 - fullPath.LastIndexOf("\\"));//帶拓展名的文件名 “Default.aspx”
string strExtensionName = fullPath.Substring(fullPath.LastIndexOf("."), fullPath.Length - fullPath.LastIndexOf("."));//擴展名 “.aspx”
3、用openFileDialog.SafeFileName,取到該文件的所在目錄路徑
string path1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + @"\";
string path = Path.GetFileName("C:\My Document\path\image.jpg"); //只獲取文件名image.jpg
4、進程相關
//獲取當前進程的完整路徑,包含文件名(進程名)。獲取包含清單的已載入文件的路徑或 UNC 位置。
string str0 = this.GetType().Assembly.Location;
//C:\Users\yiyi\AppData\Local\Temp\Temporary ASP.NET Files\web\bd33ba98\cbcc133a\App_Web_eidyl2kf.dll
//result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)
//獲取新的 System.Diagnostics.Process 組件並將其與當前活動的進程關聯
//獲取關聯進程主模塊的完整路徑,包含文件名(進程名)
string str1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exe
//result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)
//獲取或設置當前工作目錄(即該進程從中啟動的目錄)的完全限定路徑。
string str2 = System.Environment.CurrentDirectory;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
//result: X:\xxx\xxx (.exe文件所在的目錄)
//獲取當前 System.Threading.Thread 的當前應用程式域的基目錄,它由程式集衝突解決程式用來探測程式集。
string str3 = System.AppDomain.CurrentDomain.BaseDirectory;
//F:\Code\得利斯20150923\web\
//result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")
//獲取或設置包含該應用程式的目錄的名稱。
string str4 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//F:\Code\得利斯20150923\web\
//result: X:\xxx\xxx\ (.exe文件所在的目錄+"\")
//獲取啟動了應用程式的可執行文件的路徑,不包括可執行文件的名稱。
string str5 = System.Windows.Forms.Application.StartupPath;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
//result: X:\xxx\xxx (.exe文件所在的目錄)
//獲取啟動了應用程式的可執行文件的路徑,包括可執行文件的名稱。
string str6 = System.Windows.Forms.Application.ExecutablePath;
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exe
//result: X:\xxx\xxx\xxx.exe (.exe文件所在的目錄+.exe文件名)
//獲取應用程式的當前工作目錄(不可靠)。
string str7 = System.IO.Directory.GetCurrentDirectory();
//C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0
//result: X:\xxx\xxx (.exe文件所在的目錄)