C 中檔案與資料夾相關的操作

2021-04-25 00:07:44 字數 3160 閱讀 8817

///

/// 檔案有關的操作類

///

public class fileoperate

else //檔案不存在}

///

/// 獲取檔名(包含副檔名)

///

/// 檔案全路徑

/// string

public string getfilename(string filefullpath)

else}

///

/// 獲取檔案副檔名

///

/// 檔案全路徑

/// string

public string getfileextension(string filefullpath)

else}

///

/// 獲取檔名(可包含副檔名)

///

/// 檔案全路徑

/// 是否包含副檔名

/// string

public string getfilename(string filefullpath, bool includeextension)

else

}else}

///

/// 根據傳來的檔案全路徑,外部開啟檔案,預設用系統註冊型別關聯軟體開啟

///

/// 檔案的全路徑

/// bool

public bool openfile(string filefullpath)

else}

///

/// 獲取檔案大小

///

/// 檔案全路徑

/// string

public string getfilesize(string filefullpath)

else if (fl > (1024 * 1024))

else if (fl > 1024)

else

}else}

///

/// 檔案轉換成二進位制,返回二進位制陣列byte

///

/// 檔案全路徑

/// byte 包含檔案流的二進位制陣列

public byte filetostreambyte(string filefullpath)

else}

///

/// 二進位制陣列byte生成檔案

///

/// 要生成的檔案全路徑

/// 要生成檔案的二進位制 byte 陣列

/// bool 是否生成成功

public bool bytestreamtofile(string filefullpath, byte streambyte)

filestream fs = new filestream(filefullpath, filemode.openorcreate); //建立檔案流(開啟或建立的方式)

fs.write(streambyte, 0, streambyte.length); //把檔案流寫到檔案中

fs.close();

return true;

}catch}

///

/// 將xml檔案序列化(可起到加密和壓縮xml檔案的目的)

///

/// 要序列化的xml檔案全路徑

/// bool 是否序列化成功

public bool serializexml(string filefullpath)   //序列化:

catch}

///

/// 反序列化xml檔案

///

/// 要反序列化xml檔案的全路徑

/// bool 是否反序列化xml檔案

public bool deserializexml(string filefullpath) }

///

/// 與資料夾有關的操作類

///

public class diroperate

///

/// 建立資料夾

///

/// 要建立的資料夾全路徑

/// 建立資料夾時的操作選項

/// bool 是否建立成功

public bool createdir(string dirfullpath, operateoption option)

else if (option == operateoption.existdelete) //如果存在且選項為「存在刪除」

return true;

}catch}

public bool deletedir(string dirfullpath)

else

} ///

/// 得到當前資料夾中所有檔案列表

///

/// 資料夾全路徑

/// string 檔案列表

public string getdirfiles(string dirfullpath)

else}

///

/// 得到當前資料夾及下級資料夾中所有檔案列表

///

/// 資料夾全路徑

/// 搜尋選項

/// string 檔案列表

public string getdirfiles(string dirfullpath, searchoption so)

else}

///

/// 得到當前資料夾中指定檔案型別[副檔名]檔案列表

///

/// 資料夾的全路徑

/// 查詢哪種型別的檔案

/// string 檔案列表

public string getdirfiles(string dirfullpath, string searchpattern)

else}

///

/// 得到當前資料夾及下級資料夾中指定檔案型別[副檔名]檔案列表

///

/// 目錄全路徑

/// 查詢哪種型別的檔案

/// 搜尋選項

/// string 檔案列表

public string getdirfiles(string dirfullpath, string searchpattern, searchoption so)

else}}

python與檔案資料夾相關操作

在python中,檔案操作主要來自os模組,主要方法如下 os.listdir dirname 列出dirname下的目錄和檔案 os.getcwd 獲得當前工作目錄 os.curdir 返回當前目錄 os.chdir dirname 改變工作目錄到dirname os.path.isdir nam...

檔案 資料夾的相關操作

有些時候,需要對檔案進行重新命名 刪除等一些操作,python的os模組中都有這麼功能 os模組中的rename 可以完成對檔案的重新命名操作 rename 需要修改的檔名,新的檔名 import os os.rename 畢業 txt 畢業 最終版.txt os模組中的remove 可以完成對檔案...

C 中檔案與資料夾操作的類介紹

1 fileinfo類用於操作檔案 讀取檔案屬性,檢視檔案屬性,建立檔案,移動檔案,重新命名檔案,判斷路徑是否存在等。2 directoryinfo類用於操作資料夾 來讀取資料夾屬性,建立目錄,建立子目錄等。3 file類用於讀寫小檔案 讀寫檔案,可將檔案內容全部讀取出來。適合讀寫小檔案。4 使用流...