日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C# 之 获取文件名及拓展名

發布時間:2024/1/17 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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文件所在的目錄)

?

轉載于:https://www.cnblogs.com/xinaixia/p/4862851.html

總結

以上是生活随笔為你收集整理的C# 之 获取文件名及拓展名的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。