黑馬程式員:檔案操作相關類知識

來源:互聯網
上載者:User

---------------------- ASP.Net+Android+IO開發S、.Net培訓、期待與您交流! ----------------------
檔案操作相關類知識
File //操作檔案,靜態類,對檔案整體操作。拷貝、刪除、剪下等。
Directory //操作目錄(檔案夾),靜態類。
DirectoryInfo //檔案夾的一個“類”,用來描述一個檔案夾對象(擷取指定目錄下的所有目錄時返回一個DirectoryInfo數組。)
FileInfo//檔案類,用來描述一個檔案對象。擷取指定目錄下的所有檔案時,返回一個FileInfo數組。
Path//對檔案或目錄的路徑進行操作(很方便)【字串】
Stream//檔案流,抽象類別。
FileStream//檔案流,MemoryStream(記憶體流),NetworkStream(網路流)
StreamReader //快速讀取文字檔
StreamWriter//快速寫入文字檔

Path類
目錄和檔案操作的命名控制項System.IO
string ChangeExtension(string path, string extension) 
修改檔案的尾碼,“修改”支援字串層面的,沒有真的給檔案改名
string s = Path.ChangeExtension(@"C:\temp\F3.png", "jpg")
string Combine(string path1, string path2)
將兩個路徑合成一個路徑,比用+好,可以方便解決不加斜線的問題,自動處理路徑分隔字元的問題
string s = Path.Combine(@"c:\temp","a.jpg") 
string GetDirectoryName(string path) 
得到檔案的路徑名。Path.GetDirectoryName(@"c:\temp\a.jpg")
string GetExtension(string path) 得到檔案的副檔名
string GetFileName(string path) 得到檔案路徑的檔案名稱部分
string GetFileNameWithoutExtension(string path) 得到去除副檔名的檔案名稱
string GetFullPath(string path) 得到檔案的全路徑。可以根據相對路徑獲得絕對路徑。
string GetTempFileName()  得到一個唯一的臨時檔案名稱
string GetTempPath() 得到臨時檔案夾的路徑
Path.Combine(path1,path2),見MSDN
如果指定的路徑之一是零長度字串,則該方法返回其他路徑。如果 path2 包含絕對路徑,則該方法返回 path2。 
如果path1不是以分隔字元結束,並且不是c:或d:等(磁碟機引用),則在串聯前為path1增加\分隔字元。
分隔字元:(與作業系統平台有關)
Path.DirectorySeparatorChar  →  \
Path.PathSeparator  →  ;
Path.VolumeSeparatorChar  →  :
Path.GetFileName()
擷取檔案名稱
當目錄為c:\windows\test時,可擷取最後一個目錄名,但當目錄路徑為c:\windows\test\ 時,不可以。reflector查看

擷取當前exe檔案執行的路徑:
Assembly.GetExecutingAssembly().Location;
Application.StartupPath.
不要用:
Directory.GetCurrentDirectory();擷取應用程式的當前工作目錄。因為這個可能會變,通過使用OpenFileDialog或者手動設定Directory.SetCurrentDirectory()

Directory和DirectoryInfo

void Delete(string path, bool recursive)     刪除目錄, recursive表示是否遞迴刪除,如果recursive為false則只能刪除空目錄
bool Exists(string path)      判斷目錄是否存在
string[] GetDirectories(string path)  得到一個目錄下的子目錄
string[] GetDirectories(string path, string searchPattern, SearchOption searchOption)    萬用字元尋找目錄下的子目錄,可以搜尋到隱藏檔案。 
static string[] GetFiles(string path)  得到一個目錄下的檔案
string[] GetFiles(string path, string searchPattern, SearchOption searchOption)   萬用字元尋找目錄下的檔案
DirectoryInfo GetParent(string path)  得到目錄的父目錄
move()  //移動、剪下。只能在同一個磁碟中。目錄沒有Copy方法。可以使用Move()方法實現重新命名。

create()

---------------------- ASP.Net+Android+IOS開發、.Net培訓、期待與您交流! ----------------------

詳細請查看:http://edu.csdn.net

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.