C#獲取各種文件名


1、c#根據絕對路徑獲取 帶后綴文件名、后綴名、文件名。

                string str =" E:\test\Default.aspx";
                string filename = System.IO.Path.GetFileName(str);//文件名 “Default.aspx”
                string extension = System.IO.Path.GetExtension(str);//擴展名 “.aspx”
                string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(str);// 沒有擴展名的文件名 “Default”

2、c#根據絕對路徑獲取 帶后綴文件名、后綴名、文件名,使用 Split 函數。

                string str = =" E:\test\Default.aspx";
                char[] delimiterChars = { '.', '\\' };
                string[] Mystr = str.Split(delimiterChars);
                string sheetName = Mystr[Mystr.Length - 2];);// 沒有擴展名的文件名 “Default”

下面是我轉載過來的,留用。

C# 獲取文件名及擴展名(轉載自:http://www.cnitblog.com/yide/archive/2012/03/08/78003.aspx)

string aFirstName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastIndexOf(".") - aFile.LastIndexOf("\\") - 1));  //文件名
string aLastName = aFile.Substring(aFile.LastIndexOf(".") + 1, (aFile.Length - aFile.LastIndexOf(".") - 1));   //擴展名

string strFilePaht="文件路徑";
Path.GetFileNameWithoutExtension(strFilePath);這個就是獲取文件名的

還有的就是用Substring截取
 strFilePaht.Substring(path.LastIndexOf("\\") + 1, path.Length - 1 - path.LastIndexOf("\\"));
 strFilePaht.Substring(path.LastIndexOf("."), path.Length - path.LastIndexOf("."));

或者用openFileDialog1.SafeFileName

這樣就能取到該文件的所在目錄路徑
string path1 = System.IO.Path.GetDirectoryName(openFileDialog1.FileName) + @"\";

string path = Path.GetFileName("C:\My Document\path\image.jpg");    //只獲取文件名image.jpg


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


string fullPath = @"\WebSite1\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”

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

System.IO.Path.GetFileNam(filePath)       //返回帶擴展名的文件名 
System.IO.Path.GetFileNameWithoutExtension(filePath)     //返回不帶擴展名的文件名 
System.IO.Path.GetDirectoryName(filePath)     //返回文件所在目錄

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//獲取當前進程的完整路徑,包含文件名(進程名)。
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文件所在的目錄)


免責聲明!

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



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