C FileInfo類 檔案操作

2021-10-02 01:29:32 字數 1295 閱讀 8686

c# 語言中fileinfo使用類執行典型操作, 例如複製、移動、重新命名、建立、開啟、刪除和追加到檔案。

file 類是靜態類,其成員也是靜態的,通過類名即可訪問類的成員;fileinfo 類不是靜態成員,其類的成員需要類的例項來訪問。

本節主要講解 fileinfo 類的使用,在 fileinfo 類中提供了乙個構造方法,語法形式如下。

命名空間:system.io

函式:fileinfo(string filename)

功能:在這裡 filename 引數用於指定新檔案的完全限定名或相對檔名。

fileinfo 類中常用的屬性如下所示:屬性

作用directory

唯讀屬性,獲取父目錄的例項

directoryname

唯讀屬性,獲取表示目錄的完整路徑的字串

exists

唯讀屬性,獲取指定的檔案是否存在,若存在返回 true,否則返回 false

isreadonly

屬性,獲取或設定指定的檔案是否為唯讀的

length

唯讀屬性,獲取檔案的大小

name

唯讀屬性,獲取檔案的名稱

attributes

獲取或設定當前檔案或目錄的特性

creationtime

獲取或設定當前檔案或目錄的建立時間

fullname

獲取目錄或檔案的完整目錄

lastaccesstime

獲取或設定上次訪問當前檔案或目錄的時間

fileinfo 類中常用的方法如下所示:

1、filelnfo copyto(string destfilename) :將現有檔案複製到新檔案,不允許覆蓋現有檔案

2、filelnfo copyto(string destfilename, bool overwrite) :將現有檔案複製到新檔案,允許覆蓋現有檔案

3、filelnfo.create():建立檔案,返回filestream新檔案

4、void delete() :刪除檔案

5、void moveto(string destfilename): 將指定檔案移到新位置,提供要指定新檔名的選項

6、filelnfo replace(string destinationfilename, string destinationbackupfilename):使用當前檔案物件替換指定檔案的內容,先刪除原始檔案, 再建立被替換檔案的備份

檔案操作類

把乙個資料夾下所有檔案複製到另乙個資料夾下 當前資料夾 copy到新檔案 public static void copydirectory string srcpath,string destpath copydirectory i.fullname,destpath i.name 遞迴呼叫複製子資...

ini檔案操作類

using system using system.io using system.runtime.interopservices using system.text using system.collections using system.collections.specialized name...

PE檔案操作類

以前看雪發的老貼,挪一波。檔案 pragma once class pereadwrite cpp檔案 檔案 include stdafx.h include pereadwrite.h include windows.h 預設建構函式 pereadwrite pereadwrite bool pe...