在using程式碼中加入:
using System.IO;

System.IO底下的類別:
Directory 提供類別方法來建立、移動、複製資料夾......等功能
DirectoryInfo 一個資料夾就是一個DirectoryInfo物件
File 提供類別方法來建立、移動、複製檔案......等功能
FileInfo 一個檔案就是一個FileInfo物件
StreamReader 使用位元組串流來讀取文字檔
StreamWriter 使用位元組串流來寫入文字檔
FileStream 建立檔案串流,可以用來處理二進制檔

Directory類別方法:
CreateDirectory(path) 建立路徑為path的資料夾
Exists(path) 檢查路徑為path的資料夾是否存在(存在時傳回true)
Delete(path, bool) 刪除路徑為path的資料夾,bool為true時代表要刪除子資料夾
GetCurrentDirectory() 取得目前的工作路徑

示範:

//如果資料夾"D:\test"存在時就刪除它
if (Directory.Exists("D:\\test"))
{
   Directory.Delete("D:\\test",true);
}

File類別方法:
Copy(SourPath, DestPath, bool) 複製SourPath至DestPath,bool為true時表示覆寫
Delete(path) 刪除路徑為path的檔案
Exists(path) 檢查路徑為path的檔案是否存在

示範:
//檢查"D:\test.txt"是否存在,存在的話將它複製到"D:\test2.txt"
if (File.Exists("D:\\test.txt"))
{
    File.Copy("D:\\test.txt", "D:\\test2.txt", true);
}

FileInfo類別方法:
使用FileInfo時要先建立FileInfo物件//path為檔案路徑 資料型態為string
FileInfo FI = new FileInfo(path);

Name 檔案名稱
FullName 檔案全名,包含檔案路徑
Extension 檔案副檔名
Directory 取得父資料夾的DirectoryInfo物件
CreationTime 建立日期
LastAccessTime 存取日期
Length 檔案大小

示範:

//取得"D:\test.txt"的檔案大小
FileInfo FI = new FileInfo("D:\\test.txt");
MessageBox.Show(FI.Length.ToString());

arrow
arrow
    全站熱搜

    狼翔月影 發表在 痞客邦 留言(1) 人氣()